knockoutjs foreach别名不能按照页面中间knockoutjs docs解释的那样工作:
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 ) );
我只是不理解这个例子并做错了什么?它已经发生了,大声笑......
答案 0 :(得分:7)
升级到淘汰赛2.2.0并且有效。升级小提琴here。
你的装订很好 - 保持原样:
<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>