是Knockout.js的新手。
function UniqueCustomerViewModel(dataFromServer){
self.customerMasterList = ko.observableArray(@Html.Raw(Json.Encode(ViewBag.CustomerList)));
self.chosenCustomerList = ko.observableArray(dataFromServer.Customer.ChosenCustomers);
ko.utils.arrayForEach(chosenCustomerList(), function (customerRow) {
self.customerMasterList.remove(customerRow);
});
// ...
}
获取错误:0x800a138a - Microsoft JScript运行时错误:行ko.utils.arrayForEach上预期的函数
我在这里做错了什么?
答案 0 :(得分:1)
我想我发现了问题,你self
错过了chosenCustomerList
:
...
ko.utils.arrayForEach(self.chosenCustomerList(), function (customerRow) {
self.customerMasterList.remove(customerRow);
});
...
至于删除元素 - 如果它不是普通类型,则可能必须首先在self.customerMasterList()中找到元素,然后将其删除。像这样:
...
ko.utils.arrayForEach(self.chosenCustomerList(), function (customerRow) {
var customer = ko.utils.arrayFirst(self.customerMasterList(), function(item) {
return item.id === customerRow.id; // or something like thatt
});
if (customer)
self.customerMasterList.remove(customer);
});
...