1

knockoutjs foreach 别名不起作用,如页面下方的knockoutjs 文档中所述:

Note 3: Using “as” to give an alias to “foreach” items

这是一个显示错误的简单jsFiddle 。控制台说虽然我遵循了 knockoutjs 示例,但没有定义别名...

HTML

<h3>This works</h3>
<ul data-bind="foreach: people">
    <li>
        <span data-bind="text: $data.name"></span> 
        <span data-bind="text: $data.age"></span>
    </li>
</ul>

<h3>This doesn't work</h3>
<ul data-bind="foreach: { data: people, as: 'person' }">
    <li>
        <span data-bind="text: person.name"></span> 
        <span data-bind="text: person.age"></span>
    </li>
</ul>​

JS

var data = [
    { name: 'Bob', age: 35 },
    { name: 'Sue', age: 24 },
    { name: 'Rick', age: 57 }
];

var model = function( data ){
    this.people = ko.observableArray( data );
}
ko.applyBindings( new model( data ) );​

我只是不理解这个例子并且做错了什么吗?众所周知,这会发生,大声笑...

4

1 回答 1

7

升级到淘汰赛 2.2.0 就可以了。在这里升级小提琴。

您的绑定很好 - 保持原样:

<h3>This works</h3>
<ul data-bind="foreach: people">
    <li>
        <span data-bind="text: $data.name"></span> 
        <span data-bind="text: $data.age"></span>
    </li>
</ul>

<h3>This doesn't work</h3>
<ul data-bind="foreach: { data: people, as: 'person' }">
    <li>
        <span data-bind="text: person.name"></span> 
        <span data-bind="text: person.age"></span>
    </li>
</ul>​
于 2012-11-01T07:41:09.263 回答