我正在尝试在observableArray中添加元素但是在这里我遇到了新添加的元素替换最后元素的问题。
var bugTracker = function() {
var self = this;
self.bugName = ko.observable(''),
self.bugList = ko.observableArray([
{name:"Abc",age:27},{name:"Dooo",age:27},{name:"Usss",age:27},{name:"Yeess",age:27}
])
self.addBug = function() {
var newBug = {
name: self.bugName,
age: 86
}
self.bugList.push({
name: self.bugName,
age: 89
});
}
self.removeBug = function() {
self.bugList.remove(this);
}
}
bugTracker.bugList
$(function() {
ko.applyBindings(new bugTracker());
});
HTML:
<input type="text" data-bind="value : bugName" >
<input type="button" data-bind="click: addBug" value="Add Bug"/>
<ol data-bind="foreach : bugList">
<li data-bind="text : name" >
</li>
</ol>
输出:
答案 0 :(得分:1)