Ozerich
Ozerich

Reputation: 2000

knockoutJS $index is not working in condition

JS

 function NotificationsViewModel()
    {
        this.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov','Dec'];
        this.activeMonth = ko.observable(10);
    }

ko.applyBindings(new NotificationsViewModel());

HTML

<ul data-bind="foreach: months">
    <li data-bind="css:{'selected-month': $index == $root.activeMonth()}">
        <span data-bind="text: $index"></span> : <span data-bind="text: $data"></span>
    </li>
</ul>​

http://jsfiddle.net/fhZph/

Upvotes: 0

Views: 150

Answers (1)

Artem Vyshniakov
Artem Vyshniakov

Reputation: 16465

You should unwrap $index value:

<ul data-bind="foreach: months">
    <li data-bind="css:{'selected-month': $index() == $root.activeMonth()}">
        <span data-bind="text: $index"></span> : <span data-bind="text: $data"></span>
    </li>
</ul>​

Upvotes: 2

Related Questions