2014-09-04 66 views
0

在我的代碼中,我點擊添加按鈕時會顯示一個輸入欄和一個應用按鈕。點擊應用按鈕我寫了一個函數,通過它顯示我的文本和顯示字段按鈕。但我得到一個錯誤「節目是沒有定義」 這裏是我的功能可見綁定在淘汰賽js中不起作用

self.apply = function() { 
    self.show(false); 
    self.showFields(true); 
}; 

addterm這裏

self.addTerm = function() { 
    var term = new Term("12"); 
    self.Terms.push(term); 
    self.show(true); 
    self.showFields(false); 
}; 

這裏是JS小提琴Link

回答

1

當你使用迭代您Terms

<tbody data-bind="foreach: Terms"> 

Th e範圍內的tbody成爲您正在迭代的當前Term。所以,當你寫:

<input type="text" class="edit" data-bind="value: loanterm, visible: show" /> 

淘汰賽正在尋找Term.show這顯然不存在。所以,你需要指向淘汰賽使用$root關鍵字根視圖模型:

<input type="text" class="edit" data-bind="value: loanterm, visible: $root.show" /> 

(也是一樣的showFields屬性)

Binding-Context

1

您需要添加父關鍵字。

<button class="button addNewButton" data-bind="click: $root.addTerm">Add a new Term for Loan</button> 
<table class="termGrid"> 
<thead> 
    <tr> 
     <th class="headRow headColumn"> 
      Term 
     </th> 
     <th class="headRow tools"> 
     </th> 
    </tr> 
</thead> 
<tbody data-bind="foreach: Terms"> 
    <tr class="row"> 
     <td class="tools"> 

      <input type="text" class="edit" data-bind="value: loanterm, visible: $parent.show" /> 
      <strong class="read" data-bind="text: loanterm, visible: $parent.showFields" ></strong> 
      <a class="button toolButton" href="#" data-bind="visible: $parent.showFields" >show Tiers</a> 
      <a class="button toolButton" href="#" data-bind="click: $root.apply,visible:$parent.show" >Apply</a> 

     </td> 
    </tr> 
</tbody> 

fiddle demo

+0

感謝Raheel。 :) – user2142786 2014-09-04 07:34:12