簡體   English   中英

KnockoutJS可見綁定不起作用

[英]KnockoutJS visible binding not working

我被困在一個簡單的代碼,這是為了使typea可見的,只有當它類型的a 我已經用過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

我修改了小提琴-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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM