1

We can get async pipe as variable with *ngIf

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

But of course it won't work for boolean values, for example

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

Because loggedIn$ | async can emit false.

Is there any other built-in way to get async values as template variable without custom ngInit directives?

1 Answer 1

6

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

Note that *ngIf is used here not for visibility but as a way to get a template variable

Sign up to request clarification or add additional context in comments.

2 Comments

Be aware, that this makes the button instantly visible
@dewey yes, but that is expected since *ngIf is used here as a hack to get a variable in the template You basically don't need to provide anything to a callback if it is not visible And if visible - you can just provide true I've edited my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.