Angular *ngIf variable with async pipe multiple conditions

后端 未结 4 700
再見小時候
再見小時候 2021-02-12 05:31

There\'s quite good doc of using *ngIf in Angular: https://angular.io/api/common/NgIf But, is that possible to have *ngIf async variable and multiple checks on that? Something l

相关标签:
4条回答
  • 2021-02-12 06:14

    Here's the alternative version, with a little bit cleaner template:

    <ng-template [ngIf]="(users$ | async)?.length > 1" [ngIfElse]="noUsersMessage">
      <div *ngFor="let user of (users$ | async)">{{ user | json }}</div>
    </ng-template>
    
    <ng-template #noUsersMessage>No users found</ng-template>
    

    Note that we use users$ | async 2 times. That will work if you add shareReplay() operator to user$ Observable:

      public users$: Observable<any[]> = this.someService.getUsers()
        .pipe(shareReplay());
    

    That way, inner template will be able to access last value of the Observable and display the results.

    You can try it out on stackblitz.

    0 讨论(0)
  • 2021-02-12 06:15

    Simply do it like this

    <div *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1">...</div>
    

    For "more complex" scenario do the following

    <div  *ngfor="let user of users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">...</div>
    

    Edit: Previous wouldn't work since you cannot use *ngFor and *ngIf without using ng-template. You would do it like that for instance

    <ng-template ngFor let-user [ngForOf]="users$ | async" *ngIf="(users$ | async)?.length > 1 && (users$ | async)?.length < 5">
      <div>{{ user | json }}</div>
    </ng-template>
    

    Here is a stackblitz.

    0 讨论(0)
  • 2021-02-12 06:26

    I hit the same issue of needing an *ngIf + async variable with multiple checks.

    This ended up working well for me.

    <div *ngIf="(users$ | async)?.length > 0 && (users$ | async) as users"> ... </div>
    

    or if you prefer

    <div *ngIf="(users$ | async)?.length > 0 && (users$ | async); let users"> ... </div>
    

    Explanation

    Since the result of the if expression is assigned to the local variable you specify, simply ending your check with ... && (users$ | async) as users allows you to specify multiple conditions and specify what value you want the local variable to hold when all your conditions succeed.

    Note

    I was initially worried that using multiple async pipes in the same expression may create multiple subscriptions, but after some light testing (I could be wrong) it seems like only one subscription is actually made.

    0 讨论(0)
  • 2021-02-12 06:26

    I see everyone using *ngFor and *ngIf together in one tag and similar work-arounds, but I think that is an anti-pattern. In most regular coding languages, you don't do an if statement and a for loop on the same line do you? IMHO if you have to work around because they specifically don't want you to, you're not supposed to do that. Don't practice what's not "best practice."

    ✅✅✅ Keep it simple, if you don't need to declare the $implicit value from users$ | async as users:

    <!-- readability is your friend -->
    <div *ngIf="(users$ | async).length > 1"> ... </div>
    

    But if you do need to declare as user, wrap it.


    ✅ For Complex Use Case; mind you, the generated markup will not even have an HTML tag, that's the beauty of ng-templates & ng-containers!

    @alsami's accepted, edited answer works because *ngFor with asterisk is a shorthand for ng-template with ngFor (no asterisk), not to mention the double async pipe

    0 讨论(0)
提交回复
热议问题