2016-12-05 55 views
0

我被困在一个简单的代码,它使typea仅在类型为a时才可见。我已经使用了KnockoutJS。我尝试了很多,但我无法找出错误。KnockoutJS可见绑定不起作用

HTML:

<a href="#" data-bind="click: changeType('b')">change the type</a> 
<span data-bind="visible: isType('a')">a</span> 

JS:

function viewModel = { 
    var self = this; 
    self.type = ko.observable(); 
    self.isType = function(type) { 
    return type == self.type(); 
}; 
self.changeType = function(para){ 
    return function(){ 
    self.type(para); 
    } 
}; 
} 

ko.applyBindings(new viewModel()); 

JSFiddle为上述代码。

回答

1

我已经修改了小提琴 - https://jsfiddle.net/npbb333e/4/

var viewModel = function(){ 
    var self = this; 
    self.type = ko.observable(); 

    self.isType = function(type) { 
    return type === self.type(); 
    }; 

    self.changeType = function(para) { 
     self.type(para);  
    }; 
} 

ko.applyBindings(new viewModel()); 
相关问题