2017-10-06 4 views
0

I'v 한 번만 바인딩 적용 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

knockout이 해당 페이지에서 사용중인 _only_ 장소입니까? 아니면 두 개의 다른 영역에서 두 번 사용하고 있습니까? –

+0

아니요,이 페이지에서만 –

답변

1

입니다 :

  • var self = this;는 생성자 함수 내부에 있어야한다. 바깥 쪽에서는 this이 창 개체를 나타냅니다.

  • observable 속성이 포함 된 개체ko.applyBindings()에 전달해야합니다. 함수 자체가 아닙니다.

  • Function Expression or Function Declaration을 사용하면 자바 스크립트에서 함수를 만들 수 있습니다. 코드에 viewModel이 필요하지 않습니다. var vm = function() {} 또는 function vm(){}입니다.

  • 기본적으로 checkAppointmentListSelectfalse으로 설정했습니다. 클릭 할 때마다 버튼이 표시되지 않습니다.

    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 :

은 당신의 코드를 변경

. 이러한 모든 변경 사항을 적용하고 문제가 계속되는 경우 알려 주시기 바랍니다.