我正在尝试使用其中一项的属性值在可观察数组中查找值。这是一些示例代码:
HTML:
<ul data-bind="foreach: items">
<li data-bind="text:name"></li>
<li>Cousin: <span data-bind="text:related_id"></span></li>
</ul>
JS:
var item = function (data){
this.id = ko.observable(data.id);
this.name = ko.observable(data.name);
this.related_id = ko.observable(data.related_id);
}
var related_item = function(data){
this.id = ko.observable(data.id);
this.name = ko.observable(data.name);
}
var ViewModel = function(){
this.items = ko.observableArray([
new item({id:1,name:'thing1',related_id:1}),
new item({id:2,name:'thing2',related_id:2})
]);
this.related_items = ko.observableArray([
new related_item({id:1,name:'cousin it'}),
new related_item({id:2,name:'cousin fred'})
]);
}
ko.applyBindings(new ViewModel);
摆弄上面的代码:fiddle
我的问题:获取related_item 的名称属性以显示项目的related_id 当前显示在哪里的最佳方法是什么?</p>