启用绑定不起作用

时间:2017-08-28 19:06:06

标签: html css knockout.js data-binding

 <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
    <button class="btn btn-danger pull-right btn-block" data-bind="enable: DeletedEnable">Delete</button>                            
 </div>

即使DeletedEnablefalse,启用属性也不起作用,button仍处于启用状态。

self.CurrentStatusIsDraft= ko.pureComputed(function () {
        return false;
    });

self.DeletedEnable = ko.pureComputed(function () {
        return self.CurrentStatusIsDraft() ;
    });

1 个答案:

答案 0 :(得分:0)

根据您的示例https://jsfiddle.net/3rnt2zsc/

A)使用inline onClick不是一个好习惯。  由于您使用的是淘汰赛,只需使用click事件绑定即可。

B)尝试在视图中添加较少的javascript logic

C)使用模型的new实例应用绑定。

示例:https://jsfiddle.net/of8qfdvL/1/

查看:

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
   <div class="col-lg-offset-6 col-md-offset-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
      <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
         <button class="btn btn-default btn-gb-default btn-block pull-right" data-bind="enable: ContinuedEnabled, click:OnContinue">Continue</button>
      </div>
      <button class="btn btn-danger pull-right btn-block" data-bind="enable: DeletedEnabled(), click:OnDelete">Delete</button>
      <pre data-bind="text: ko.toJSON(DeletedEnabled, null, 2)"></pre>
   </div>
</div>

<强>型号:

var viewModel = function() {
   var self = this;
   self.SubmissionId = ko.observable(1234);
   self.LastPageId = ko.observable(2);
   self.ContinuedEnabled = ko.observable(true);  // you can set the value based on your logic

   self.OnContinue = function(){
      console.log("SubmissionId:",self.SubmissionId());
      console.log("LastPageId:",self.LastPageId());
   }

   self.OnDelete = function(){
      console.log("SubmissionId:",self.SubmissionId());
   }

   self.CurrentStatusIsDraft= ko.pureComputed(function () {
       return false;
   });

   self.DeletedEnabled = ko.pureComputed(function () {
      return self.CurrentStatusIsDraft() ;
   });
}

ko.applyBindings(new viewModel()); // apply a new instance of your model