简体   繁体   English

数据绑定不适用于div的条件显示

[英]data-bind not working for conditional display of a div

I have the following very simple code sample to test the conditional display of a div. 我有以下非常简单的代码示例来测试div的条件显示。 For some reason it's not working and I was wondering if anyone had encountered it. 由于某种原因,它无法正常工作,我想知道是否有人遇到过它。 I'm using knockout-3.0.0.js. 我正在使用淘汰赛3.0.0.js.

Here is my HTML 这是我的HTML

<div data-bind="if: displayDetail">
    HERE IS THE DETAIL <label data-bind="text: displayDetail"></label>
</div>

<div data-bind="ifnot: displayDetail">
    THERE IS NO DETAIL <label data-bind="text: displayDetail"></label>
</div>
<button data-bind='click: flip'>Flip</button>

and here is my Javascript 这是我的Javascript

function BooleanViewModel() {
var self = this;
self.displayDetail = ko.observable(false);

// Operations
self.flip = function() {
    //alert("val is " + self.displayDetail);
    if(self.displayDetail){
        self.displayDetail = false;
    }else{
        self.displayDetail = true;        
    }
};

}
ko.applyBindings(new BooleanViewModel());

Kindly let me know if you see anything that I have missed 请让我知道是否看到我错过的任何内容

Each observable is a function so to get or set value you should use () : 每个observable是一个函数,因此要获取或设置值,您应该使用()

self.flip = function() {
    //alert("val is " + self.displayDetail());
    if(self.displayDetail()){
        self.displayDetail(false);
    }else{
        self.displayDetail(true);        
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM