Async pipe not working with Subject

后端 未结 2 1417
终归单人心
终归单人心 2021-02-14 16:46

I have the following BehaviorSubject in a service:

  isAuthenticated = new BehaviorSubject(false);

And I am using it as follows

相关标签:
2条回答
  • 2021-02-14 17:17

    I thought of posting a solution using ng-if-else which is maybe even more intuitive in your particular case:

    <li class="login-button" *ngIf="(authenticated | async); else unauthenticated">
      <a>Logged in</a>
    </li>
    <ng-template #unauthenticated>
      <a (click)="authenticate()">Log in</a>
    </ng-template>
    

    Alternatively you could puth both cases inside a ng-template:

    <li class="login-button" *ngIf="(authenticated | async); then authenticated else unauthenticated"></li>
    <ng-template #authenticated ><a>Logged in</a></ng-template>
    <ng-template #unauthenticated><a (click)="authenticate()">Log in</a></ng-template>
    

    Hope it is of any use to other people ending up here.

    0 讨论(0)
  • 2021-02-14 17:39

    I suspect its the order of operations - you need parenthesis around your subscription:

    <li class="login-button" *ngIf="!(authenticated | async)">
    
    0 讨论(0)
提交回复
热议问题