angular - 管道转换中的 promise

标签 angular typescript ionic-framework angular-pipe

我不想使用“|异步管道”,因为我不想获取每个组件的数据。我的转换函数如下:

 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/

相关文章:

javascript - 在 Chrome 中调试 TypeScript,404 响应

javascript - 如何在Restangular中添加多个Auth Interceptor?

css - 键盘将模态弹出窗口中的文本字段推到顶部

ios - Cordova 中是否有关于应用程序即将卸载或更新的事件?

angular - 有没有办法隐藏 <mat-paginator> 的长度,使其为 "1-10"而不是 "1-10 of 100"?

javascript - 使用换行符中断 HTML 工具提示消息

找不到 Angular EventEmitter

Angular2和RXJS,收到一次数据后取消订阅

node.js - typescript jwt.verify 无法访问数据

'as' 关键字实际上做了什么?