我正在尝试做下一件事:
<div data-bind="foreach: { data: elementsVM.elementsList, as: 'element' }">
<div data-bind="with: new ElementViewModel(element).getElementDetailsByLang(langFilter)">
<h3 data-bind="html: ElementTitle"></h3>
</div>
</div>
问题是,当with: new ElementViewModel(element).getElementDetailsByLang(langFilter)
被调用时,element
作为参数结束ElementViewModel(Element)
的是来自 foreach 绑定 () 的整个集合,elementsVM.elementsList
而不是来自 foreach 迭代的当前元素。elementsVM 是一个 ElementsViewModel:
function ElementsViewModel() {
var self = this;
self.ElementsList = ko.observableArray();
self.getElementsForCategory = function(categoryId) {
[...]
}
}
ElementViewModel 看起来像这样:
function ElementViewModel(Element) {
var self = this;
self.ElementModel = new ElementModel(Element);
self.getElementDetailsByLang = function (lang) {
return ko.computed(function () {
[...]
}
}
}