Angular2 使用 Observables From Other Sources

原文链接:https://angular-2-training-book.rangle.io/handout/observables/using_observables_from_other_sources.html

Using Observables From Other Sources

In the example above we created Observables from scratch which is especially useful in understanding the anatomy of an Observable.

+

However, we will often create Observables from callbacks, promises, events, collections or using many of the operators available on the API.

+

Observable HTTP Events

A common operation in any web application is getting or posting data to a server. Angular applications do this with the Http library, which previously used Promises to operate in an asynchronous manner. The updated Http library now incorporates Observables for triggering events and getting new data. Let’s take a quick look at this:

+

import {Component} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/Rx';

@Component({
    selector: 'app',
    template: `
      <b>Angular HTTP requests using RxJs Observables!</b>
      <ul>
        <li *ngFor="let doctor of doctors">{{doctor.name}}</li>
      </ul>

      `
})

export class MyApp {
  private doctors = [];

  constructor(http: Http) {
    http.get('http://jsonplaceholder.typicode.com/users/')
        .flatMap((data) => data.json())
        .subscribe((data) => {
          this.doctors.push(data);

        });
  }
}

View Example

+

This basic example outlines how the Http library’s common routines like get, post, put and delete all return Observables that allow us to asynchronously process any resulting data.

+

Observable Form Events

Let’s take a look at how Observables are used in Angular forms. Each field in a form is treated as an Observable that we can subscribe to and listen for any changes made to the value of the input field.

+

import {Component} from '@angular/core';
import {FormControl, FormGroup, FormBuilder} from '@angular/forms';
import 'rxjs/add/operator/map';

@Component({
    selector: 'app',
    template: `
      <form [formGroup]="coolForm">
            <input formControlName="email">
        </form>
      <div>
            <b>You Typed Reversed:</b> {{data}}
        </div>
    `
})

export class MyApp {

    email: FormControl;
    coolForm: FormGroup;
    data: string;

    constructor(private fb: FormBuilder) {
        this.email = new FormControl();

        this.coolForm = fb.group({
            email: this.email
        });

        this.email.valueChanges
        .map(n=>n.split('').reverse().join(''))
        .subscribe(value => this.data = value);
    }
}

View Example

+

Here we have created a new form by initializing a new FormControl field and grouped it into a FormGroup tied to the coolForm HTML form. The Control field has a property .valueChanges that returns an Observable that we can subscribe to. Now whenever a user types something into the field we’ll get it immediately.

+

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值