KnockOut 循环无法绑定单击事件

问题描述

问题出在 data-bind='click: selecttag这句代码
浏览器报错 selecttag is not define
但以前一直是这么写的呀..没出过问题..怎么今天就报错了呢....

<ul class="file-tags-ul">
    <!-- ko foreach: filetags -->
    <li data-bind="attr: {style:leftlinear},css:{isselect}">
        <a href="javascript:void(0);" data-bind='click: selecttag,attr: {title: descsum },text: desc'></a>
        <div data-bind="attr: {style:linear}"></div>
    </li>
    <!-- /ko -->
</ul>

js

var FileViewModel = function() {
        var self = this;
    
    self.selecttag = function(){
        console.log(this)
    };
}

解决方法

去掉前面的 self. 就好了.

js

var FileViewModel = function() {
        var self = this;
    
    selecttag = function(){
        console.log(this)
    };
}

推荐阅读更多精彩内容