Using Observables

Let's take a look at a basic example of how to create and use an Observable in an Angular component:

  1. import {Component} from '@angular/core';
  2. import {Observable} from 'rxjs/Observable';
  3. @Component({
  4. selector: 'app',
  5. template: `
  6. <b>Angular Component Using Observables!</b>
  7. <h6 style="margin-bottom: 0">VALUES:</h6>
  8. <div *ngFor="let value of values">- {{ value }}</div>
  9. <h6 style="margin-bottom: 0">ERRORs:</h6>
  10. <div>Errors: {{anyErrors}}</div>
  11. <h6 style="margin-bottom: 0">FINISHED:</h6>
  12. <div>Finished: {{ finished }}</div>
  13. <button style="margin-top: 2rem;" (click)="init()">Init</button>
  14. `
  15. })
  16. export class MyApp {
  17. private data: Observable<Array<number>>;
  18. private values: Array<number> = [];
  19. private anyErrors: boolean;
  20. private finished: boolean;
  21. constructor() {
  22. }
  23. init() {
  24. this.data = new Observable(observer => {
  25. setTimeout(() => {
  26. observer.next(42);
  27. }, 1000);
  28. setTimeout(() => {
  29. observer.next(43);
  30. }, 2000);
  31. setTimeout(() => {
  32. observer.complete();
  33. }, 3000);
  34. });
  35. let subscription = this.data.subscribe(
  36. value => this.values.push(value),
  37. error => this.anyErrors = true,
  38. () => this.finished = true
  39. );
  40. }
  41. }

View Example


First we import Observable into our component from rxjs/Observable. Next, in our constructor we create a new Observable. Note that this creates an Observable data type that contains data of number type. This illustrates the stream of data that Observables offer as well as giving us the ability to maintain integrity of the type of data we are expecting to receive.

Next we call subscribe on this Observable which allows us to listen in on any data that is coming through. In subscribing we use three distinctive callbacks: the first one is invoked when receiving new values, the second for any errors that arise and the last represents the function to be invoked when the sequence of incoming data is complete and successful.

We can also use forEach to listen for incoming data. The key difference between forEach and subscribe is in how the error and completion callbacks are handled. The forEach call only accepts the 'next value' callback as an argument; it then returns a promise instead of a subscription.

When the Observable completes, the promise resolves. When the Observable encounters an error, the promise is rejected.

You can think of Observable.of(1, 2, 3).forEach(doSomething) as being semantically equivalent to:

  1. new Promise((resolve, reject) => {
    Observable.of(1, 2, 3).subscribe(
    doSomething,
    reject,
    resolve);
    });


The forEach pattern is useful for a sequence of events you only expect to happen once.

  1. export class MyApp {

    private data: Observable<Array<number>>;
    private values: Array<number> = [];
    private anyErrors: boolean;
    private finished: boolean;

    constructor() {
    }

    init() {
    this.data = new Observable(observer => {
    setTimeout(() => {
    observer.next(42);
    }, 1000);

    setTimeout(() => {
    observer.next(43);
    }, 2000);

    setTimeout(() => {
    observer.complete();
    }, 3000);

    this.status = "Started";
    });

    let subscription = this.data.forEach(v => this.values.push(v))
    .then(() => this.status = "Ended");
    }

    }


View Example

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