淘汰赛:更新 observableArray 中的项目
Knockout: Update item in an observableArray
我正在尝试更新 observableArray 中的对象,如下所示:
var vm = {
tests: ko.observableArray([{input: 'bar'}])
};
vm.tests.push(ko.observable({input: 'foo'}));
ko.applyBindings(vm);
setTimeout(function () {
vm.tests()[1]().input = '123';
}, 500);
出于某种原因,setTimeout
中应用的更改未反映在 DOM 中。任何建议为什么?而且,有没有办法用 knockout
来观察数组以及它包含的值?
使项目的输入 属性 也成为可观察的:{input: ko.observable("foo")}
然后用 setter vm.tests()[1]().input("123")
:
更新它
var vm = {
tests: ko.observableArray([{input: 'bar'}])
};
vm.tests.push(ko.observable({input: ko.observable('foo')}));
ko.applyBindings(vm);
setTimeout(function () {
vm.tests()[1]().input('123');
}, 1000);
我正在尝试更新 observableArray 中的对象,如下所示:
var vm = {
tests: ko.observableArray([{input: 'bar'}])
};
vm.tests.push(ko.observable({input: 'foo'}));
ko.applyBindings(vm);
setTimeout(function () {
vm.tests()[1]().input = '123';
}, 500);
出于某种原因,setTimeout
中应用的更改未反映在 DOM 中。任何建议为什么?而且,有没有办法用 knockout
来观察数组以及它包含的值?
使项目的输入 属性 也成为可观察的:{input: ko.observable("foo")}
然后用 setter vm.tests()[1]().input("123")
:
var vm = {
tests: ko.observableArray([{input: 'bar'}])
};
vm.tests.push(ko.observable({input: ko.observable('foo')}));
ko.applyBindings(vm);
setTimeout(function () {
vm.tests()[1]().input('123');
}, 1000);