Subscribing to an observable in an Angular 2 HTML element

前端 未结 2 1402
滥情空心
滥情空心 2021-01-19 04:48

I have an observable producing Users that have an isLoading property, such that this yields the expected result:

{{ (user$ | async)         


        
相关标签:
2条回答
  • 2021-01-19 04:52

    One option you have here, if you've upgraded to Angular 4, is to use *ngIf. You can do something like this:

    <div *ngIf="user$ | async; let user">
    
    {{user.isLoading}}    
    
    <button md-raised-button type="submit" [disabled]="user.isLoading">Login</button>
    
    </div>
    

    With *ngIf you can subscribe to the observable with async and assign the value to a variable with let. Then user the variable in other places in your template.

    This is a new feature added in Angular 4. Most Angular2 apps can be upgraded to 4 with no changes required.

    Hope that helps.

    0 讨论(0)
  • 2021-01-19 05:04

    You can use async pipe in html tag like in example below, just don't forget safe operator:

    <button md-raised-button type="submit" [disabled]="(user$ | async)?.isLoading">Login</button>
    
    0 讨论(0)
提交回复
热议问题