0

错误:无法解析绑定。消息:ReferenceError:消息未定义;绑定值:文本:消息。

我无法弄清楚绑定失败的原因。

下面是javascript代码:

    var LogItemModel = function () {
        var self = this;

        function LogItem($root, appName, level, message) {
            self.appName = ko.observable(appName);
            self.level = ko.observable(level);
            self.message = ko.observable(message);
        }

        self.logItems = ko.observableArray();

        self.add = function (appName, level, message) {
            self.logItems.push(new LogItem(self, appName, level, message));
        };
    };


    $(document).ready(function () {
        var logItemModel = new LogItemModel();
        ko.applyBindings(logItemModel);
        var url = "http://localhost:49091/api/Log";

        $.getJSON(url, function (result) {
            $.each(result, function (idx, logItem) {
                logItemModel.add(logItem.ApplicationName, logItem.Level, logItem.Message);
                debugger;
            });
        }).error(function (e) {
        });

    });

下面是html代码:

<div id="LogItemList" data-bind="foreach:logItems">
    <span data-bind="text: message"></span>
</div>

我什至尝试将绑定应用于 LogItemList 元素

ko.applyBindings(logItemModel, $("#LogItemList")[0]);

但即使这样也没有用。

4

1 回答 1

2
function LogItem($root, appName, level, message) {
    self.appName = ko.observable(appName);
    self.level = ko.observable(level);
    self.message = ko.observable(message);
}

在此函数中,在 LogItemModel 中引用了 self,将其更改为:

function LogItem($root, appName, level, message) {
    var that = this;
    that.appName = ko.observable(appName);
    that.level = ko.observable(level);
    that.message = ko.observable(message);
}
于 2013-11-04T13:08:52.327 回答