替换observableArray中的项目 [英] Replacing item in observableArray
问题描述
我正在尝试用新内容替换 observableArray
中项目的所有内容。
I'm trying to replace all of the contents of an item in an observableArray
with new content.
var oldLocation = ko.utils.arrayFirst(self.locations(), function (item) {
return item.id == value.id;
});
self.locations.replace(self.locations.indexOf(oldLocation), new location(value));
self.locations.valueHasMutated();
我也试过
self.locations[self.locations.indexOf(location)] = new fizi.ko.models.location(value);
但没有任何效果。索引正在被正确检索,但项目的更新没有发生。
But nothing is working. The index is being properly retrieved but the update of the item isn't happening.
推荐答案
替换函数接受两个参数,要替换的项目以及要替换它的新项目。您正在传递索引以代替要替换的项目,因此它不起作用。
The replace function accepts two parameters, the item you want to replace and the new item you want to replace it with. You are passing in the index in place of the item to replace so it doesn't work.
替换调用应该是:
self.locations.replace(oldLocation, new location(value));
在旁注中,您不需要 valueHasMutated()
在那里调用,它将被 replace()
调用调用。
On a side note, you shouldn't need the valueHasMutated()
call there, it will get invoked by the replace()
call.
这篇关于替换observableArray中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!