Salvador Dali
Salvador Dali

Reputation: 222461

Can not make css binding to work in knockout

I am trying to make a simple example with css binding in knockout. After reading documentation, I thought that this will yield correct result.

Basically I have a simple model:

function ArticlesViewModel(){
    this.popularTags = [{
        id: 1,
        s: ko.observable(false)
    },{
        id: 2,
        s: ko.observable(false)
    },{
        id: 2,
        s: ko.observable(true)
    }];
}

ko.applyBindings(new ArticlesViewModel());

And I want to add a particular class based on the the s property. So I tried the following view but with no result:

<span data-bind="foreach: popularTags">
    <span class="square" data-bind="text: id, css: { selected: s == false}"></span>
</span>

Can someone enlighten what I am doing wrong?

Upvotes: 0

Views: 142

Answers (1)

Tanner
Tanner

Reputation: 22733

You just need to unwrap the observable selected: s() == false: http://jsfiddle.net/f6BCT/2/

<span class="square" data-bind="text: id, css: { selected: s() == false}"></span>

Upvotes: 1

Related Questions