knockout: Uncaught TypeError: Object #<Object> has no method 'newCommentText'

☆樱花仙子☆ 提交于 2020-01-14 10:42:18

问题


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:

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.


回答1:


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.



来源:https://stackoverflow.com/questions/11022829/knockout-uncaught-typeerror-object-object-has-no-method-newcommenttext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!