简体   繁体   English

Angular:如何在模板 function 中传递 boolean 异步变量?

[英]Angular: How to pass boolean async variable in template function?

We can get async pipe as variable with *ngIf我们可以使用 *ngIf 将异步 pipe 作为变量

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

But of course it won't work for boolean values, for example但当然它不适用于 boolean 值,例如

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

Because loggedIn$ | async因为loggedIn$ | async loggedIn$ | async can emit false. loggedIn$ | async可以发出 false。

Is there any other built-in way to get async values as template variable without custom ngInit directives?是否有任何其他内置方法可以在没有自定义 ngInit 指令的情况下将异步值作为模板变量获取?

Just found an answer刚刚找到答案

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

Maybe a little bit hacky but works也许有点hacky但有效

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

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