2017-10-06 30 views
0

心中已經應用結合只有一次,但仍然得到錯誤你不能多次申請綁定到相同的元素Knockout.Js

不能多次申請綁定同一個元素。

這是我的腳本。

<script> 
var self = this; 
     var vm = function viewModel() { 
       self.getAppointment = function() { 
        $("#dialog-confirm ").dialog({ 
         resizable: false, 
         height: 250, 
         width: 500, 
         modal: true 
        }); 

        self.checkAppointmentListSelect(true); 
       } 

       self.checkAppointmentListSelect = ko.observable(false); 

       self.btnSelectAppointmentClick = function() { 
        self.checkAppointmentListSelect(true); 
       } 
       debugger; 
      } 

      ko.applyBindings(vm); 
    </script> 

這是HTML數據

<div id="dialog-confirm" title="Select Appointment"> 
     <div class="modal-body" data-bind="visible: checkAppointmentListSelect"> 

      <button class="btn btn-primary" id="btnSelectAppointment" data-bind="click: btnSelectAppointmentClick">Select</button> 
      <button class="btn btn-primary" id="btnCancel">Cancel</button> 
     </div> 

     <div class="modal-body" data-bind="visible: checkAppointmentListSelect"> 

      <button class="btn btn-primary" id="btnSelectAppointment">Select </button> 
      <button class="btn btn-primary" id="btnCancel">Cancel</button> 
     </div> 
    </div> 
+0

此代碼工作正常,您可以添加您的視圖代碼示例?你有更多的地方,你叫ko.applyBindings? –

+0

該頁面上正在使用淘汰賽的唯一地點,還是您在兩個不同區域使用了兩次? –

+0

nope,只在此頁 –

回答

1

幾件事情需要注意:

  • var self = this;應在構造函數中。外,this指的是窗口對象。

  • 您應該通過對象包含observable屬性到ko.applyBindings()。不是功能本身。

  • 您可以使用Function Expression or Function Declaration在javascript中創建函數。您的代碼中的viewModel不是必需的。它可以是 var vm = function() {}function vm(){}

  • 默認情況下,您已將checkAppointmentListSelect設置爲false。您的按鈕將不會顯示在加載中供您點擊。

你的代碼更改爲:

function vm() { 
    var self = this; // this should be inside the vm function 

    self.getAppointment = function() { 
    $("#dialog-confirm ").dialog({ 
     resizable: false, 
     height: 250, 
     width: 500, 
     modal: true 
    }); 

    self.checkAppointmentListSelect(true); 
    } 

    self.checkAppointmentListSelect = ko.observable(true); 

    self.btnSelectAppointmentClick = function() { 
    self.checkAppointmentListSelect(true); 
    } 
} 

ko.applyBindings(new vm()); // `new vm()` creates an object of type vm 

Here's a fiddle。如果你仍然面臨任何問題,請進行所有這些更改並讓我知道。

相關問題