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 ) );
我只是不理解这个例子并且做错了什么吗?众所周知,这会发生,大声笑...