1

In the previous *ngIf directives we were able to chain multiple async operations to not repeat the async pipe in the template but to reuse them later as one subscription, now I want to use the same behavior using the new @if syntax provided in Angular 17, is it possible?

Old way:

<ng-container *ngIf="{
   test: test$ | async,
   anotherOne: anotherOne$ | async,
   // and so on...
} as data">
  <span *ngIf="data.test.length">Show something</span>
</ng-container

I want to replace the code above to use only @if statement

@if({
   test: test$ | async,
   anotherOne: anotherOne$ | async,
   // it doesn't work that way
} as data) {
   <!-- show something -->
}

1 Answer 1

2

You need the ; (semicolon) before as.

@if({ test: test$ | async, anotherOne: anotherOne$ | async }; as data) { 
  <!-- show something -->
}

Reference: Angular - @if (Description)

Demo @ StackBlitz

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

2 Comments

Yeah that's the character I was missing :D It works! Thanks!
Yeah I was searching in the wrong tab that's why I couldn't find it in the official documentation, it's under Reference tab also for the new documenation, angular.dev/api/core/@if

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.