【发布时间】:2015-06-26 01:47:27
【问题描述】:
在搜索互联网时,作为我的视图模型一部分的以下代码应该可以正常工作:
function MyViewModel() {
var self = this;
self.name = ko.observable();
self.displayname = ko.observable();
self.description = ko.observable();
self.save = function () {
$.post('/My/MVCControler',
{ Name: self.name(), DisplayName: self.displayname(), Description: self.description() },
function success(data) {
self.name(null);
self.displayname(null);
self.description(null);
$('#MyDialog').modal('hide');
});
}
}
但是我在Name: self.name() 收到错误Uncaught TypeError: string is not a function。
最终目标是在表单提交时重置视图模型。作为最佳实践,我还希望只调用一次 ko.applyBindings。
编辑:在得到很好的回答后,我意识到 this.name 是由我的代码以外的某个地方的字符串创建的。更改变量名称解决了这个问题。
【问题讨论】:
-
您确定您没有在其他地方无意中为它分配了一个字符串吗?发布的代码看起来不错。
-
你试过了吗:self.name('');???
-
我确实尝试过
self.name('')。仍然不确定发生了什么。
标签: knockout.js