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

I have code like this in my view model:function ChatListViewModel(chats) {var self = this;self.newComme

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 ment:

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

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 ment:

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

Share Improve this question asked Jun 13, 2012 at 20:29 ladookieladookie 1,3714 gold badges21 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745322853a4622520.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信