我有一個非常簡單的視圖模型:淘汰賽可見約束力不工作
var ViewModel = function() {
this.showRow = ko.observable(false);
this.toggleVisibility = function() {
if(this.showRow == true){
this.showRow = false;
}
else{
this.showRow = true;
}
alert('showRow is now '+this.showRow); //only here for testing
};
};
用同樣簡單的標記:
<a href="#" data-bind="click: toggleVisibility">Toggle</a>
<br />
<table>
<tr data-bind="visible: showRow">Some Text</tr>
</table>
我的問題是,當點擊鏈接時,警報框顯示(顯示正確的值 - 真/假)
但是,tr
元素上的可見綁定看起來不起作用 - 最初(該行應該在加載時不可見),或者值爲showRow
切換。
<table>
<tr data-bind="visible: showRow"><td>Some Text</td></tr>
</table>
和JavaScript如下:
var ViewModel = function() {
var self = this;
self.showRow = ko.observable(false);
self.toggleVisibility = function() {
self.showRow(!self.showRow());
alert('showRow is now ' + self.showRow());
};
};
ko.applyBindings(new ViewModel());
語法設定值可觀察到的屬性是
的上文http://jsfiddle.net/alexjamesbrown/FgVxY/3/
'this.showRow ='表示您正在覆蓋「可觀察」屬性。當您使用observable時,該屬性是一個函數,而不僅僅是一個值。 –
要添加到James D'Angelo的觀點,如果要更新observable屬性的值,請將該值作爲參數傳遞給屬性。 'this.showRow(false)'; – Quickhorn