在我的ViewModel我有:
self.collaps = ko.observableArray([0,0,0,0,0,0,0]);
self.shouldShow = function(index) { return self.collaps()[index]; };
我的测试DIV:
<div data-bind="visible: shouldShow(5)">Shown!</div>
我data-bind
一个按钮click: show
:
self.show = function() {
// 1. Index 5 gets true after 2 clicks!!? But UI never updates!
self.collaps()[5] = true;
// 2. This is push-ing in true starting at index 0
self.collaps.replace(self.collaps()[5], true);
// 3. If I combine the two, I get the behavior I want, I think :)
self.collaps()[5] = true;
self.collaps.replace(self.collaps()[5], true);
};
这是怎么回事吗? 什么是这样做的正确方法吗?
----> 的jsfiddle为您的乐趣! <----