0

当我从服务器获取数据时,我无法在我的 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
[] 
4

1 回答 1

0

你不需要itemViewModelinsidesuccess方法,

success: function (data) {
                    item = data;
                    $.each(data.Comments, function (index) {
                        comments.push(data.Comments[index]);
                        console.log(data.Comments[index]);
                        console.log(comments);
                    });
                    isLoaded(true);
                    itemDetailBindings();
                    console.log(item);
                    console.log(comments);
                }
于 2012-08-24T17:00:56.187 回答