4

I have code like this in my view model:

function ChatListViewModel(chats) {
    var self = this;

    self.newCommentText = ko.observable();

    self.addComment = function(chat) {
      var newComment = { CourseItemDescription: this.newCommentText() };
      chat.CommentList.push(newComment);
      self.newCommentText("");       
    };

}

ko.applyBindings(new ChatListViewModel(initialData));

but I get this error when I try to add a new comment:

enter image description here

any Ideas what I'm doing wrong? I looked at some knockout samples on the knockoutjs.com webpage and this is how they were doing it.


Try this.

self.addComment = function(chat) {
   var newComment = { CourseItemDescription: self.newCommentText() };
   chat.CommentList.push(newComment);
   self.newCommentText("");       
};

Your this variable is not what you expect.

Hope this helps.

4

1 回答 1

5

尝试这个。

self.addComment = function(chat) {
   var newComment = { CourseItemDescription: self.newCommentText() };
   chat.CommentList.push(newComment);
   self.newCommentText("");       
};

您的 this 变量不是您所期望的。

希望这可以帮助。

于 2012-06-13T20:50:07.317 回答