2013-11-04 72 views
0

錯誤:無法解析綁定。消息:ReferenceError:消息未定義;綁定值:文本:消息。
KnockOut無法解析綁定。 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]);

但即使這樣也行不通。

回答

2
function LogItem($root, appName, level, message) { 
    self.appName = ko.observable(appName); 
    self.level = ko.observable(level); 
    self.message = ko.observable(message); 
} 
在此功能自

在LogItemModel被引用,改變它,以便:

function LogItem($root, appName, level, message) { 
    var that = this; 
    that.appName = ko.observable(appName); 
    that.level = ko.observable(level); 
    that.message = ko.observable(message); 
} 
+0

非常感謝,這個工作。 – user2952571