我不想使用“|异步管道”,因为我不想获取每个组件的数据。我的转换函数如下:
transform(key: string, ...args) {
this.storage.get("dictionary").then((dict) => {
var translated = dict[key] || "noResource";
console.log("key = "+key + ", value = "+translated);
return translated;
});
}
我可以获取键和值,但未呈现值。我试过 ngZone
但没有用。
最佳答案
我不明白为什么您不想使用唯一符合您情况的“内置”管道。
说你的管道是:
@Pipe({name: 'mypipe'})
export class MyPipe implements PipeTransform
{
transform(key: string, ...args) {
// watch out you missed the return statement !!
-->return<-- this.storage.get("dictionary").then((dict) => {
var translated = dict[key] || "noResource";
console.log("key = "+key + ", value = "+translated);
return translated;
});
}
在你的模板中你可以直接使用
{{ 'mykey' | mypipe | async }}
如果您不想使用异步管道,您将被迫模仿已经非常简单且防错的逻辑。对你没有好处。
关于angular - 管道转换中的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48637853/