为什么在 angular 中使用 observables 2

Why observables are used in angular 2

我在angular写服务的时候,反复看到一个词叫'Observable'。 我的界面,

 export interface IDetails{
              custominfo:string;
    }

我的服务,

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { IDetails } from './data';

@Injectable()
export class GetAllList {
    id =  this.loc._id;
    private _productUrl = 'http://localhost:3000/getprofilebyid/'+this.id;

    constructor(private _http: Http) { }
    getList(): Observable<IDetails[]> {
        return this._http.get(this._productUrl)
        .map((response: Response) => {  return <IDetails[]> response.json().data; 
        });

    }
}

我有2个疑问,

1)为什么我的所有变量都是在名为 'interface'.

的文件中声明的 http 调用结果

2)我的代码中'Observable'这个词有什么用

谁能提前help.Thanks

Observable 是 Promise 的强大替代品。 您可以将其视为事件流。因此,每当可观察对象发生事件时,订阅者都会收到通知。

示例:

getList(): Observable<IDetails[]> {
        return this._http.get(this._productUrl)
        .map((response: Response) => {  return <IDetails[]> response.json().data; 
        });

    }

这里的 getList() 方法有一个 return 类型的 Observable,可以订阅它以获取包含 IDetails 类型元素的数组。

用法:

export class SomeComponent {
    constructor(private getAllList:GetAllList)
    this.getAllList.getList().subscribe(
        (data)=>{
            //do something with the array containing elements of type IDetails
            console.log(data);
        }),
        (error)=>{
            // handle error
            console.log(error);
        }),
        ()=>{
           // On Compete
           console.log("Subscription completed")
        })
}

进一步阅读: http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html