![](/img/trans.png)
[英]How to make 'independent' computed observable of some observable (Knockout.js)
[英]Knockout.js - how do I get the value of an observable property inside a computed observable?
我有以下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)
};
但是mydunamicfield
属性没有产生正确的连接结果。 如果我尝试在另一个函数中引用this.description()
,我会在加载页面时看到以下错误消息:
Property 'description' of object [object Window] is not a function
这种情况有什么问题?
首先,如果要获取其值,则必须将this.description
作为this.description()
引用。
其次,尝试将computed
字段放在viewModel
之外(作为'this'
,即viewModel
本身未在创建computed
observable时定义。
有关工作示例,请参见http://jsfiddle.net/rAEqK/2/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.