除非您指定'write'选项,否则无法将值写入ko.computed [英] Cannot write a value to a ko.computed unless you specify a 'write' option
本文介绍了除非您指定'write'选项,否则无法将值写入ko.computed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在另一个计算属性中使用计算属性,当我运行代码时,控制台出现以下错误.
I am trying to use computed properties in another computed properties and when i run code i am getting following error in console.
除非您指定写"选项,否则无法将值写入ko.computed
Cannot write a value to a ko.computed
unless you specify a 'write' option
function AppViewModel() {
var self = this;
self.firstName = ko.observable('rahul');
self.lastName = ko.observable('sharma');
self.fullName = ko.computed(function() {
return self.firstName() +' ' + self.lastName();
});
self.upperFullName = ko.computed(function() {
return self.fullName.toUpperCase();
});
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());
这是html代码和 js小提琴链接
<p><input data-bind="value: firstName"></p>
<p><input data-bind="value: lastName"></p>
<p><input data-bind="value: fullName"></p>
<p> <span data-bind="text: upperFullName"> </span> </p>
推荐答案
self.fullName是一个函数,返回计算出的值.
self.fullName is a function, returning the computed value.
self.upperFullName = ko.computed(function() {
return self.fullName().toUpperCase();
});
注意括号!
这篇关于除非您指定'write'选项,否则无法将值写入ko.computed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文