繁体   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