9

I'm used to use the async pipe with "as" in an Angular HTML template to avoid replicating observable subscriptions, like this:

<component *ngIf="(selected$ | async) as selected"></component>

So then I can use "selected" anywhere else in the template.

But then if I try to use it like this, in an input:

<component [param]="(selected$ | async) as selected"></component>

I get an error:

Unexpected token 'as' at column 21 in [categories$ | async as categories]

Any idea why? Thanks!

2 Answers 2

10

That's correct, the as syntax is specific to *ngIf. It's not a general keyword you can use anywhere in Angular templates.

See https://angular.io/api/common/NgIf and search for NgIfAs class.

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

1 Comment

How do I use a async var to disable a button. Do we need to use a function?
2

As already martin says as syntax is specific to *ngIf. But you can use ng-container to get results that you want:

<ng-container *ngIf="(selected$ | async) as selected">
    <component [param]="selected"></component>
</ng-container>

Comments

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.