Angular:如何在模板函数中传递布尔异步变量?

时间:2019-11-11 09:58:10

标签: angular templates rxjs

我们可以使用* ngIf

将异步管道作为变量
<button *ngIf="account$ | async as account" (click)="parseAccount(account)" type="button"></button>

例如,当然,它不适用于布尔值

<button *ngIf="loggedIn$ | async as loggedIn" (click)="checkAuth(loggedIn)" type="button"></button>

因为loggedIn$ | async可以发出false。

还有其他内置方法可以在没有自定义ngInit指令的情况下将异步值作为模板变量获取吗?

1 个答案:

答案 0 :(得分:1)

刚刚找到答案

<button *ngIf="{ val: loggedIn$ | async } as loggedIn" (click)="checkAuth(loggedIn.val)" type="button"></button>

也许有点hacky,但是可以工作