【问题标题】:KnockoutJS visible binding not workingKnockoutJS 可见绑定不起作用
【发布时间】:2017-04-19 11:24:35
【问题描述】:

我被困在一个简单的代码上,它是使 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 以上代码。

【问题讨论】:

    标签: javascript html knockout.js


    【解决方案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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      相关资源
      最近更新 更多