我有以下代码:
var ObjectViewModel = function (testObject) {
//debugger;
var self = this;
self.id = testSet.id;
self.details = testOject.details;
self.children = ko.observableArray(testObject.children);
self.childCount = ko.computed(function() {
return self.children().length;
});
self.addObject = function () {
//debugger;
// Pending UI
// Call API here
// On success, complete
self.children.push(dummyObject);
self.childToAdd("");
}.bind(self);
}
/ etc
然而在 childCount 中,this.children() 是未定义的。我试图让视图实时显示子数组的长度,因此当用户添加/删除项目时,计数会更新。知道为什么这不起作用吗?