Javascript js-如何在计算出的可观察属性中获取可观察属性的值?
我有以下Knockout.js对象:Javascript js-如何在计算出的可观察属性中获取可观察属性的值?,javascript,knockout.js,Javascript,Knockout.js,我有以下Knockout.js对象: var viewModel = { description : ko.observable(""), Name : ko.observable(""), productid : ko.observable(""), productmodel : ko.observable(""), productnumber : ko.observable(""), text_relevance : ko.observable
var viewModel = {
description : ko.observable(""),
Name : ko.observable(""),
productid : ko.observable(""),
productmodel : ko.observable(""),
productnumber : ko.observable(""),
text_relevance : ko.observable(""),
mydunamicfield : ko.computed(function() {
return "bq=(and " +
((this.description == "") ? "" : ("description:" + this.description + " ")) +
")";
} , this)
};
但是mydunamicfield
属性没有生成正确的连接结果。如果我尝试在另一个函数中引用this.description()
,则在加载页面时会看到以下错误消息:
Property 'description' of object [object Window] is not a function
这种情况下有什么问题?首先,如果要获取其值,必须将
this.description
引用为this.description()
其次,尝试将computed
字段放在viewModel
之外(因为'this'
是viewModel
本身在创建computed
可观察时未定义)
有关工作示例,请参见