[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());
  }
}
时间: 2024-08-03 19:31:13

[Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword的相关文章

angular.bind() 函数

angular.bind bind 函数有三个参数, 参一:是一个对象 参二:是一个 function 参三:是用来给参二传参数的,可写可不写,看你心情 参数也可以在调用函数的时候传,也可以当做第三个参数传 在函数的体内可以访问参数一的所有属性值 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document<

我的第一篇博客随笔:关于angularjs API的理解心得。angular.bind() angular.boostrap()

angular.bind(obj,fun,args) obj:对象 fun:函数 arg:函数要传递的参数(可省略). 解释:将函数与对象动态绑定在一起 作用:实现调用数据模块化. demo:var Func = function(a){ this.a=a; } var obj=new Func(1); var fun=angular.bind(obj, function(i,j){alert(this.a+i+j)},4,2); fun();//7  var fun=angular.bind(

function angular.bind()

解释:返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能.格式:angular.bind(self,fn,args);self:object 对象: fn的上下文对象,在fn中可以用this调用fn:function: 绑定的方法args:传入fn的参数 var obj = { name: "Any" }; var fn = function (Adj) { console.log(this.name

angular.bind()

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h1>angular.bind</h1> <p> <span>使用方法:</span><br /> <span>angular.bind(s

[从 0 开始的 Angular 生活]No.38 实现一个 Angular Router 切换组件页面(一)

前言 今天是进入公司的第三天,为了能尽快投入项目与成为团队可用的战力,我正在努力啃官方文档学习 Angular 的知识,所以这一篇文章主要是记录我如何阅读官方文档后,实现这个非常基本的.带导航的网页应用. 需求 需求大概是这样的: 开一个新的 Angular 项目,并且一开始选择加入 Router 功能 根组件是 AppComponent ,然后下方有三个子组件分别是 page1 page2 page3 可以在 AppComponent 内点击连结切换到这三个页面 参考文档: 路由与导航 Rou

[Angular 2 Router] Configure Your First Angular 2 Route

Using the Angular 2 router requires defining routes, passing them in to the RouterModule.forRoot and then importing the configured RouterModule into your main App Module. Use the Wiki Search as example project. Create a HomeComponent to contain every

Angular项目构建指南 - 不再为angular构建而犹豫不决(转)

如果你不知道什么是Angular或者根本没听说过,那么我接下来所说的对你来说毫无益处,不过如果你打算以后会接触Angular或者干脆要涨涨姿势~读下去还是有点用的. Angular和它之前所出现的其余前端框架最大的不同,在于它的核心不再是DOM,而是数据,是model.我们惯用的不管是单纯的jQuery还是MVC的Backbone,它们本质仍是让我们更方便更有条理的操作DOM,但是Angular不是.通过一系列魔术般的手法,它将一切的重心转移到数据上.以开发应用而不是操作节点的方式去开发Web,

ES6, Angular,React和ABAP中的String Template(字符串模板)

String Template(字符串模板)在很多编程语言和框架中都支持,是一个很有用的特性.本文将Jerry工作中使用到的String Template的特性做一个总结. ES6 阮一峰老师有一个专门的网站介绍ES6入门,还出了一本书: <ECMAScript6标准入门>. http://es6.ruanyifeng.com/ 我们来看看ES6里的String Template. 首先看下面这段代码. <html> <div id="JerryTest"

Angular 2 HTTP Requests with Promise

第一步:模拟restful api,还是以英雄列表为例.(我用的是node+express模拟,禁用同源策略)没什么好说的直接上代码. var express = require('express'); var app = express(); //设置跨域访问式一 app.all('*', function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header(&