簡體   English   中英

Knockout.js - 如何在計算的observable中獲取可觀察屬性的值?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM