淘汰印刷未定义模板foreach

时间:2012-10-19 07:08:18

标签: javascript knockout.js

这是一个非常简单的例子,我有一个带有数组的基本JavaScript对象,我使用knockout映射来使数组可观察等。

但是当我应用我的绑定时,我只会得到未定义。

任何想法为什么?

HTML

<div data-bind="with: Data">

    <div class="tab-pane active" id="studydirections">
        <div data-bind="template: { name: studydirectionstemplate, foreach: studydirections }">
        </div>
    </div>

</div>

<script id="studydirectionstemplate" type="text/html">
    <div> <span data-bind="text: Name"> </span> </div> 
</script>​

的JavaScript

var viewModel = {};

var data = {
    studydirections: [{
        RecId: 299,
        ColtechCode: 75,
        Name: "FARM",
        Description: "FARMING"},
    {
        RecId: 306,
        ColtechCode: 12,
        Name: "BACC",
        Description: "ACCOUNTING"},
    {
        RecId: 334,
        ColtechCode: 11,
        Name: "BMAN",
        Description: "BUSINESS MANAGEMENT"
    }
]};

viewModel.Data = ko.mapping.fromJS(data);

ko.applyBindings(viewModel);​

结果是

undefinedundefinedundefined

这是一个用来说明问题的小提琴的链接

http://jsfiddle.net/armandvdwalt/3Q267/

1 个答案:

答案 0 :(得分:3)

只需更改,将模板名称括在单引号中。

<div data-bind="template: { name: 'studydirectionstemplate', foreach: studydirections }">

检查更新的 jsFiddle