我正在尝试通过点击removePollOption
函数重新分配foreach中的对象值
<div data-bind="foreach: pollOptions">
<input data-bind="value: title">
<div data-bind="text: destroy">
<a href='#' data-bind='click: $root.removePollOption'></a>
</div>
pollOptions
数组:
this.pollOptions = ko.observableArray(ko.utils.arrayMap(optionsInitialData, function(pollOption) {
return { id: pollOption.id, title: pollOption.title, destroy: pollOption.destroy };
}));
但是当我尝试在函数值中执行它时,动态不会改变
this.removePollOption = function() {
this.destroy = true;
};
如果我尝试this.destroy(true);
,我会收到错误Uncaught TypeError: boolean is not a function
答案 0 :(得分:0)
我明白我应该将destroy
声明为可观察的
this.pollOptions = ko.observableArray(ko.utils.arrayMap(optionsInitialData, function(pollOption) {
return { id: pollOption.id, title: pollOption.title, destroy: ko.observable(false) };
}));