![](/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.