[英]Knockout.js - how do I get the value of an observable property inside a computed observable?
I have following Knockout.js object: 我有以下Knockout.js对象:
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)
};
But the mydunamicfield
property isn't producing the the correct concatenated result. 但是mydunamicfield
属性没有产生正确的连接结果。 If I try to reference this.description()
inside another function, I see the following error message when the page is loading: 如果我尝试在另一个函数中引用this.description()
,我会在加载页面时看到以下错误消息:
Property 'description' of object [object Window] is not a function
What is the problem in this case? 这种情况有什么问题?
Firstly, you must reference this.description
as this.description()
if you want to get its value. 首先,如果要获取其值,则必须将this.description
作为this.description()
引用。
Secondly, try putting your computed
field outside your viewModel
(as 'this'
which is the viewModel
itself isn't defined at the point you create the computed
observable. 其次,尝试将computed
字段放在viewModel
之外(作为'this'
,即viewModel
本身未在创建computed
observable时定义。
See http://jsfiddle.net/rAEqK/2/ for a working example. 有关工作示例,请参见http://jsfiddle.net/rAEqK/2/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.