简体   繁体   English

Angular2:带异步管道的日期管道

[英]Angular2: date pipe with async pipe

I'm setting this code in order to print out a subscription feed date field: 我设置此代码是为了打印出订阅供稿日期字段:

<div class="col-xs-8">
  <span class="text-light fs-mini m">
    {{((user$ | async).validation) | date: 'dd/MM/yyyy'}}
  </span>
</div>

Angular is telling me: Angular告诉我:

ERROR Error: Uncaught (in promise): TypeError: co.user is undefined 错误错误:未捕获(承诺):TypeError:co.user未定义

I've to say that the problem appears when I've added date pipe. 我必须说,当我添加date管道时就会出现问题。 (user$|async).valitation works fine without formatting. (user$|async).valitation无需格式化(user$|async).valitation正常工作。

((user$ | async).validation) resolves to undefined which is then passed to the date pipe. ((user$ | async).validation)解析为undefined ,然后传递给日期管道。

I would suggest you resolve what either with a subscription in your component and then: 我建议您通过在组件中进行subscription来解决问题,然后:

 <div class="col-xs-8">
  <span class="text-light fs-mini m">
    {{ user.validation | date: 'dd/MM/yyyy' }}
  </span>
</div>

Or something similar to this: 或类似的东西:

 <div class="col-xs-8">
  <span *ngIf="user$ | async" class="text-light fs-mini m">
    {{ user$.validation | async | date: 'dd/MM/yyyy' }}
  </span>
</div>

Or create a custom date pipe that accepts observables as parameters. 或创建一个接受日期作为参数的自定义日期管道

As of Angular 6 (I thinkl) You could use the as syntak too on the async. 从Angular 6开始(我认为),您也可以在异步上使用as语法。

 <div class="col-xs-8">
  <span *ngIf="user$ | async as user" class="text-light fs-mini m">
    {{ user.validation | date: 'dd/MM/yyyy' }}
  </span>
</div>

This article is worth a read Handling Observables with NgIf and the Async Pipe 值得阅读NgIf和Async Pipe处理可观察对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM