当我从服务器获取数据时,我无法在我的 observableArray 中插入任何值,但其他元素获取数据。
JS标记:
var itemViewModel = {
item: {},
isLoaded: ko.observable(false),
comments: ko.observableArray([]),
loadcontent: function (getID) {
$.ajax({
url: '/api/item/details/' + getID,
dataType: 'json',
success: function (data) {
itemViewModel.item = data;
$.each(data.Comments, function (index) {
itemViewModel.comments.push(data.Comments[index]);
console.log(data.Comments[index]);
console.log(itemViewModel.comments);
});
itemViewModel.isLoaded(true);
itemDetailBindings();
console.log(itemViewModel.item);
console.log(itemViewModel.comments);
}
});
}
};
结果:
Object
[]
Object
[]
Object
[]
[]
Object
[]