Knockout.js 互动版

foreach

使用foreach绑定可以方便我们循环遍历输出某个数组、集合中的内容。如下代码实现循环遍历输出数组。

<table> 
     <thead> 
         <tr><th>First name</th><th>Last name</th></tr> 
     </thead> 
     <tbody data-bind="foreach: people"> 
         <tr> 
             <td data-bind="text: firstName"></td> 
             <td data-bind="text: lastName"></td> 
         </tr> 
     </tbody> 
</table>
ko.applyBindings({
         people: [
             { firstName: 'Bert', lastName: 'Bertington' },
             { firstName: 'Charles', lastName: 'Charlesforth' },
             { firstName: 'Denise', lastName: 'Dentiste' }
         ]
});
练一练多个属性字段的绑定。如:people:[{firstName:'Jim',lastName:'Green',fullName:'Jim Green'}].