您可以使用self.unreadcount
可觀察值來設置count1
屬性。
self.searchFilters = ko.observableArray([
{label: "A", active: ko.observable(false), count1: self.unreadcount},
{label: "B", active: ko.observable(false), count1: self.unreadcount},
{label: "C", active: ko.observable(false), count1: self.unreadcount}
]);
See fiddle
或者可以將綁定在count1
屬性的$parent.unreadcount
:
<div data-bind="foreach : searchFilters">
<span data-bind="text : label"></span>
<span data-bind="text : $parent.unreadcount"></span>
<br/>
</div>
JS
var VM = function() {
var self = this;
self.unreadcount = ko.observable();
self.searchFilters = ko.observableArray([
{label: "A", active: ko.observable(false)},
{label: "B", active: ko.observable(false)},
{label: "C", active: ko.observable(false)}
]);
};
See fiddle
如果您需要手動更新count1
;你可以這樣做:
self.searchFilters = ko.observableArray([
{label: "A", active: ko.observable(false), count1: ko.observable(self.unreadcount())},
{label: "B", active: ko.observable(false), count1: ko.observable(self.unreadcount())},
{label: "C", active: ko.observable(false), count1: ko.observable(self.unreadcount())}
]);
self.update = function(newValue) {
ko.utils.arrayForEach(self.searchFilters(), function(row) {
row.count1(newValue);
});
}
See fiddle
添加的jsfiddle演示。 – alexmac