KnockoutJS – 不用ko foreach的bind array方法

http://jsfiddle.net/G9T3u/13/

HTML:

<ul id=’tw’/>

JavaScript:

var li_array = [];
li_array.push({item:’A’});
li_array.push({item:ko.observable(‘B’)});
li_array.push({item:’C’});

var inner_html = “;
for(i=0, max =li_array.length; i<max; i++){
inner_html += ‘<li data-bind="text: items()[‘+i+’].item"/>’;
}
$(‘#tw’).html(inner_html);
var viewModel = function(list){
this.items = ko.observableArray(list);
}
ko.applyBindings(new viewModel(li_array));
li_array[1].item(‘b’);

廣告