0

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

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

任何想法为什么?

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/

4

1 回答 1

3

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

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

检查您更新的jsFiddle

于 2012-10-19T07:12:48.763 回答