knockout:Uncaught TypeError:Object#< Object>没有方法'newCommentText' [英] knockout: Uncaught TypeError: Object #<Object> has no method 'newCommentText'

查看:74
本文介绍了knockout:Uncaught TypeError:Object#< Object>没有方法'newCommentText'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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.

推荐答案

试试这个。

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

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

Your this variable is not what you expect.

希望这会有所帮助。

这篇关于knockout:Uncaught TypeError:Object#< Object>没有方法'newCommentText'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆