简体   繁体   中英

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. For some reason it's not working and I was wondering if anyone had encountered it. I'm using knockout-3.0.0.js.

Here is my 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

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 () :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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