簡體   English   中英

Angular管道內可觀察到的HTML過濾請求

[英]Observable request inside Angular pipe for HTML filtering

我知道有異步管道,通過使用可觀察對象,您可以訂閱它們並在它們更新時獲取值。

我正在嘗試創建一個簡單的管道,該管道將使用其代碼獲取分類器的翻譯,但它使用可觀察的方式進行轉換,因此是異步的。 我可以以某種方式等待aync操作完成然后返回結果嗎? 最佳方法是什么? 我認為異步管道不是我想要的,因為基本上它是具有自己的HTML的組件。

@Pipe({
    name: 'translate'
})
export class Translate implements PipeTransform {

    constructor(public translateContext: TranslateContext) {
    }

    transform(value: any, classification: string): any {
     this.translateContext.getTranslation(classification).subscribe(res => {
            return res.get(value.toString());
        });
    }
}

我想在HTML中使用以下管道,如下所示

{{code | translate: classificator }}

我如何解決這個問題,從管道中獲得該值。

return res.get(value.toString());

改成:

transform(value: any, classification: string): any {
 return this.translateContext.getTranslation(classification).map(res => {
        return res.get(value.toString());
    });
}

並像這樣使用它:

{{code | translate:classificator | async }}

這里的想法是返回一個包含您的值的可觀察對象,然后使用async訂閱該可觀察對象。 這將在您的模板中顯示映射的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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