0
我試圖使用knockout applyBindingsToNode來動態添加和刪除元素。原因是我需要更新我的HTML,所以綁定將被削減,這就是爲什麼我需要使用applyBindingsToNode。如何解決knockout applyBindingsToNode添加多個元素?
我在這裏有一個例子,如果你點擊按鈕重新綁定名稱,它會添加四個元素,但我只期望2.任何我想念的?
這裏是我的jsfiddle http://jsfiddle.net/noppanit/dhztqgbz/
var Model = function Model() {
var people = ko.observableArray([{
name: 'name1'
}, {
name: 'name2'
}]);
var changeElement = function() {
people.push({
name: 'name3'
});
};
var rebindElement = function() {
var peopleList = document.getElementById('people_list');
ko.cleanNode(peopleList);
ko.applyBindingsToNode(peopleList, {
foreach: [{
name: 'name3'
}, {
name: 'name4'
}]
});
};
return {
people: people,
changeElement: changeElement,
rebindElement: rebindElement
};
};
ko.applyBindings(new Model(), document.getElementById('wrapper'));
我更新我的建議的jsfiddle,但它仍然沒有工作。 http://jsfiddle.net/noppanit/dhztqgbz/1/ – toy 2014-09-22 13:36:41