Domanda

Sto restituendo JSON da un servizio AJAX e sto vincendo i risultati usando Knockoutjs.

Uno dei miei attacchi è un'immagine e ho bisogno di parte dell'URL che provenga dal ViewModel come così: https://graph.facebook.com/ ' + FacebookPageId +'/Picture? Type = Large '} "/>

Se copi e incolla il risultato del tag immagine costruito, fornisce un'immagine valida da una pagina Facebook. Ma se legato all'attributo SRC sembra spogliare i caratteri non alfa-numerici dal percorso.

Ecco un jsfiddle del mio problema http://jsfiddle.net/8rb8v/1/

Ecco HTML:

<h1>Events</h1>
<button id="btnSearch">Click to Search</button>
<div class="myresult" data-bind="foreach: events">
    <div>
        Name: <span data-bind="html: Name"></span><br/>
        facebookPageID: <span data-bind="html: facebookPageID"></span><br/>
        Constructed Image Tag: <span data-bind="html: 'https://graph.facebook.com/' + facebookPageID + '/picture?type=large'"></span><br/>
        <img width=100 height=100 border=1 data-bind "attr:{ src: 'https://graph.facebook.com/' + facebookPageID + '/picture?type=large' }" />
    </div>
</div>

Ecco il codice:

var jsonData = {
        "events": [{
            "Name": "London marathon",
            "facebookPageID": "71506449747"
        }, {
            "Name": "Great North Run",
            "facebookPageID": "71506449747"
        }]
    };

// View model declaration
var EvViewModel = {
    events: ko.observableArray([])
};

function getEvents() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/echo/json/',
        data: { json: JSON.stringify(jsonData) },
        success: function (data) {
            EvViewModel.events(data.events);
            $("myresult").fadeIn();
        },
    });
}

$(document).ready(function () {
    $('#btnSearch').click(getEvents);
    ko.applyBindings(EvViewModel);
});

Grazie

È stato utile?

Soluzione

Il tuo markup non è valido:

<img width=100 height=100 border=1 data-bind "attr:{ src:
                                            ^
                                            missing an equals sign

Potrebbe essere possibile che il browser stia cercando di girare il data-bind Valore dell'attributo in una parte di un nome di attributo, eliminando così i caratteri non alfanumerici.

Inoltre, dovresti sempre Usa le virgolette attorno ai valori degli attributi:

<img width="100" height="100" border="1" data-bind="..." />
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top