knockout.js 我是第一次使用Knockout js,我尝试显示一个列表,但收到以下错误

kg7wmglp  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(129)

未捕获的引用错误:无法处理绑定“foreach:函数(){返回列表}”


$(function () {
    ko.applyBindings(modelView);
    modelView.viewCourses();
});
var modelView = {
    Courses: ko.observableArray([]),
    viewCourses: function () {
        var thisObj = this;
        try {
            $.ajax({
                url: '/Home/ListaConcepto',
                type: 'GET',
                dataType: 'json',
                contentType: 'application/json',
                success: function (data) {
                    thisObj.lista(data); 
                },
                error: function (err) {
                    alert(err.status + " : " + err.statusText);
                }
            });
        } catch (e) {
            window.location.href = '/Home/Index/';
        }
    }
};
h7appiyu

h7appiyu1#

发布thisObj.lista(data);lista的代码的问题不存在于thisObj上。需要添加它。

$(function () {
    ko.applyBindings(modelView);
    modelView.viewCourses();
});
var modelView = {
    Courses: ko.observableArray([]),
    viewCourses: function () {
        var thisObj = this;
        thisObj.lista = ko.observableArray(); // adding this line should make it work.
        try {
            $.ajax({
                url: '/Home/ListaConcepto',
                type: 'GET',
                dataType: 'json',
                contentType: 'application/json',
                success: function (data) {
                    thisObj.lista(data); 
                },
                error: function (err) {
                    alert(err.status + " : " + err.statusText);
                }
            });
        } catch (e) {
            window.location.href = '/Home/Index/';
        }
    }
};

相关问题