我无法在html中获取子字段值.Below是代码。“results”是我从Web服务获取的数组。值被推送到子数组中,我能够成功地发出警报但在绑定时无法在html中获取它们。
视图:
<h2>People</h2>
<ul data-bind="foreach: people">
<li>
<div>
<span data-bind="text: name"> </span> has <span data-bind='text: children().length'> </span> children:
</div>
<ul data-bind="foreach: {data: children, as: 'child'}">
<li>
<span data-bind="text: child.name"> </span>
<span data-bind="text: child.age"> </span>
<span data-bind="text: child.weight"> </span>
</li>
</ul>
视图模型:
var Person = function(name, children, len) {
this.name = name;
this.children = new ko.observableArray(children);
this.children.push({
name: children.name;
age: children.age;
weight: children.weight;
});
}
}
var viewModel = function() {
var this = self;
self.callServicekids("10",retKids(results) {
for (var i=0; i<results.length ; i++){
new person("Annabelle", results[i] );
}
},errorHandler);
}
ko.applyBindings(viewModel);