2016-12-05 56 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()); 
相關問題