Knockout没有调用该函数可见

时间:2012-05-22 05:56:48

标签: javascript jquery knockout.js

我想说,

<ul data-bind="foreach: items">
    <li>        
        <span data-bind="text: name"></span>
            <span data-bind="visible: $parent.checkVisiblity"></span>
    </li>
</ul>

var ViewModel = function() {
this.items = [{name: 'a'},{name: 'b'}];
 this.checkVisiblity = function(item) {
     console.log(item)
     return item.name === 'a';
     }
};

ko.applyBindings(new ViewModel());

为什么敲门不会调用函数checkVisiblity?怎么称呼它。

http://jsfiddle.net/cPVGg/1/

1 个答案:

答案 0 :(得分:4)

visible绑定更改为:

<span data-bind="visible: $parent.checkVisibility($data)"></span>