2012-08-08 172 views
0

我想用這樣的嵌套模板結合+淘汰賽JS

Question 1 
     Option1 
     Option2 
     Option3 
Question 2 
     Option1 
     Option2 
     Option3 
Question 3 
     Option2 
     Option3 

任何一個基因敲除映射到綁定模板,請提供一些示例代碼 thax任何幫助

+0

重複爲[http://stackoverflow.com/questions/11859563/nested-knockout-template-binding/11859990#11859990](http://stackoverflow.com/questions/ 11859563 /嵌套敲除模板結合/ 11859990#11859990) – vadim 2012-08-08 09:25:45

回答

3

這是簡單examle:

HTML代碼:

<script type="text/html" id="SimpleTemplate"> 
    <li> 
     <div> 
      <span data-bind="text: name"> 
      options: 
     </div> 
     <ul data-bind="foreach: option"> 
      <li> 
       <span data-bind="text: $data"> </span>    
      </li> 
     </ul> 
    </li> 
</script> 
<ul data-bind=" 
      template: { 
      name: 'SimpleTemplate', 
      foreach: questions 
      }"> 
</ul> 

敲除:

var Question= function(name, option) { 
    this.name = name; 
    this.option = ko.observableArray(option); 
}; 

var viewModel = { 
    questions: [ 
     new Question("Question1", ["option1", "option2", "option3"]), 
     new Question("Question2", ["option1", "option2", "option3"]), 
     new Question("Question3", ["option1", "option2"]) 
     ]  
}; 

ko.applyBindings(viewModel);