knockout:Uncaught TypeError:Object#<Object>没有方法'newCommentText'

lad*_*kie 4 javascript knockout.js

我的视图模型中有这样的代码:

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));
Run Code Online (Sandbox Code Playgroud)

但是当我尝试添加新评论时出现此错误:

在此输入图像描述

任何想法我做错了什么?我在knockoutjs.com网页上查看了一些淘汰样本,这就是他们这样做的方式.

mad*_*kay 5

试试这个.

self.addComment = function(chat) {
   var newComment = { CourseItemDescription: self.newCommentText() };
   chat.CommentList.push(newComment);
   self.newCommentText("");       
};
Run Code Online (Sandbox Code Playgroud)

你的这个变量不是你所期望的.

希望这可以帮助.