Angular *ngIf variable with async pipe multiple conditions

后端 未结 4 2107
粉色の甜心
粉色の甜心 2021-02-12 05:46

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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-12 06:30

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

    
      
    {{ user | json }}
    No users found

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

      public users$: Observable = 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.

提交回复
热议问题