我不明白为什么这里的第二项没有渲染,即只有一个 p 得到输出..:
<div class="row" data-bind="foreach: Foos">
<div class="three columns">
<p data-bind="text: CriteriaStub"></p>
<p data-bind="text: CriteriaStub"></p>
</div>
<div class="three columns">
<!--this gets rendered fine-->
<p data-bind="text: Baa"></p>
</div>
</div>
它似乎只发生在连续绑定元素上。
查看 foreach 的模型项:
var Foo= (function () {
function Foo(jsonObject) {
var self = this;
self.AdHocRecipients = ko.observableArray(jsonObject.AdHocRecipients);
self.CriteriaStub = ko.computed(function () {
return "No criteria";
});
self.AdHocRecipientsToStringStub = ko.computed(function () {
var stub = "";
for(var i in self.AdHocRecipients()) {
stub += (self.AdHocRecipients()[i].User) + ', ';
}
return self.AdHocRecipients().length > 0 ? stub.slice(0, -2) : "No recipiants";
});
var t = 2;
}
return Foo;
})();
家长:
var ViewModel = (function () {
function ViewModel(json) {
var parsedFoos = new Array();
for(var i in json.Foos) {
parsedFoos.push(new Foo(json.Foos[i]));
}
this.Foos = ko.observableArray(parsedFoos);
return ViewModel;
})();