谁能解释为什么这不起作用?
function MyViewModel() {
var data = [
{ forename: "Bob", age: 23 },
{ forename: "Alex", age: 19 },
{ forename: "Joe", age: 67 }
];
var persons = ko.observableArray(data);
useForEach = function () {
ko.utils.arrayForEach(persons, function (item) {
item.age(99);
});
};
return {
persons: persons,
useForEach: useForEach
}
}
var vm = new MyViewModel();
ko.applyBindings(vm);
我希望我的观点能更新年龄,但什么也没有发生……这是我的观点:
<button data-bind="click: useForEach ">useForEach</button>
<table>
<thead>
<tr>
<th>Forename</th>
<th>Age</th>
</tr>
</thead>
<tbody data-bind="foreach: persons">
<tr>
<td data-bind="text: forename"></td>
<td data-bind="text: age"></td>
</tr>
</tbody>
</table>