remove不是可观察数组中的函数

时间:2014-02-06 12:20:31

标签: javascript knockout.js ko.observablearray

我在knockoutjs中创建了一个observableArray,我想从该数组中删除项目并尝试

self.work_days = ko.observableArray();

self.work_days().push(new WorkDayVM({}, date))
//WorkDayVM is a view model and date is date object this works fine and values are //assigned well in array.

但是当试图删除时

self.work_days.remove(days_to_remove[i]);
//also tried as self.work_days().remove(days_to_remove[i]);

我也试过

for(var i = 0; i < days_to_remove.length; i++){
 self.work_days.remove(function(item){
   return item.work_days.day_string == days_to_remove[i].day_string;
 });
}

但我总是得到同样的错误

self.work_days().remove is not a function

2 个答案:

答案 0 :(得分:2)

self.work_days().remove is not a function

尝试直接在observableArray上调用.remove()(和.push()),而不是尝试在数组值上调用它。 self.work_days.remove instead

self.work_days().remove

http://jsfiddle.net/NNU77/

答案 1 :(得分:1)

self.work_days()。push(新的WorkDay VM({},date))应该是self.work_days.push(新的WorkDay VM({},date))然后你应该在没有()的情况下进行删除