0

我想像这样使用敲除映射来绑定模板

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

任何人请提供一些示例代码以获取任何帮助

4

1 回答 1

3

这是一个简单的例子:

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);
于 2012-08-08T08:16:22.990 回答