Knockout JS:在foreach中设定价值

时间:2015-03-06 22:19:27

标签: javascript knockout.js

我正在尝试通过点击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

1 个答案:

答案 0 :(得分:0)

我明白我应该将destroy声明为可观察的

this.pollOptions = ko.observableArray(ko.utils.arrayMap(optionsInitialData, function(pollOption) {
  return { id: pollOption.id, title: pollOption.title, destroy: ko.observable(false) };
}));