简体   繁体   English

KnockoutJS可见绑定不起作用

[英]KnockoutJS visible binding not working

I'm stuck at a simple code which is to make the type that is a visible only if it of type a . 我被困在一个简单的代码,这是为了使typea可见的,只有当它类型的a I' have used KnockoutJS for the same. 我已经用过KnockoutJS了。 I have tried a lot but I'm not able to find out the mistake. 我已经尝试了很多,但无法找出错误。

HTML: HTML:

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

JS : 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 for above code. 上述代码的JSFiddle

I have modified the fiddle - https://jsfiddle.net/npbb333e/4/ 我修改了小提琴-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