首页 > 代码库 > [Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword
[Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword
Angular allows us to conveniently use the async
pipe to automatically register to RxJS observables and render the result into the template once the request succeeds. This has some drawbacks, especially if we want to bind the same data in multiple parts of the template. In this lesson we will learn how we can leverage the async
pipe and the as
keyword introduced in Angular version 4.0.0 to circumvent such drawbacks.
import { Component, OnInit } from ‘@angular/core‘;import { Http } from ‘@angular/http‘;import ‘rxjs/add/operator/map‘;@Component({ selector: ‘person-detail‘, template: ` <h1>Person Detail</h1> <div *ngIf="person$ | async as person"> Name: {{ person.name }} <br /> Twitter: {{ person.twitter }} </div> `})export class PersonDetailComponent implements OnInit { person$; constructor(private http: Http) { } ngOnInit() { this.person$ = this.http .get(‘./person.json‘) .map(res => res.json()); }}
[Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。