Angular *ngIf variable with async pipe multiple conditions

后端 未结 4 695
再見小時候
再見小時候 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:26

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

    This ended up working well for me.

    ...

    or if you prefer

    ...

    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.

提交回复
热议问题