我想循环一个 observableArray 的对象,在这个循环中,我必须得到另一个 observableArray 的对应值。我没有成功地做到这一点。
我的代码如下。
模型和视图模型:
//**********MODEL********************
function Configuration() {
var self = this;
self.properties = ko.observableArray();
}
function deviceProperty() {
var self = this;
self.property = ko.observable("");
self.value = ko.observable("");
}
//**********VIEWMODEL****************
function compareModelView() {
var self = this;
self.config1 = ko.observable(new Configuration);
self.config2 = ko.observable(new Configuration);
$().ready(function () {
//Load config1 and config2
});
}
//**********jQuery********************
(function ($) {
$(document).ready(function () {
ko.applyBindings(new compareModelView());
});
})(jQuery);
看法:
<!-- ko foreach: config1().properties -->
<tr>
<td data-bind="text: property()"></td>
<td data-bind="text: value()"></td>
<td data-bind="text: $root.config2().properties()[$index].value()"></td>
</tr>
<!-- /ko -->
Knockoutjs 报错,说这个属性不存在。
当我这样做时$root.config2().properties().length
,它会返回一个数字 (3)。当我这样做时$root.config2().properties()[0]
,它会返回 [Object][Object]。当我这样做时$root.config2().properties()[0]
,它返回一个空字符串。
但是我看不到如何直接处理对象内部的 value 属性?