如何在 AngularJS 2.0 中发出 HTTP 请求

How to make HTTP requests in AngularJS 2.0

我只想知道如何在新的 angularjs 2.0 中发出 http 请求以及如何将此服务挂接到控制器。

谢谢。

截至目前,http module/component 仍在开发中,尚未发布。在此期间,有两种选择可供使用:

您可以在此处了解有关新 http 服务的更多信息:

我认为已经添加了基本支持。基于此 example:

  1. Bootstrap 你的应用带有来自 'angular2/http' 的 httpInjectables //index.ts

  2. 在构造函数constructor中注入Http(http:Http) //http_comp.ts

  3. http.get('./people.json').map(res => res.json()).subscribe(people => this.people = people);

摘录如下:

import {Component, View} from 'angular2/core';
import {Http, HTTP_BINDINGS} from 'angular2/http';
import {bootstrap} 'angular2/platform/browser';
import {CORE_DIRECTIVES,NgFor} from 'angular/common';

    @Component({
       selector: 'app'
    })

    @View({
        templateUrl: 'devices.html',
        directives: [CORE_DIRECTIVES,NgFor]
    })

    export class App {
        devices: any;
        constructor(http: Http) {
            this.devices = []; 
            http.get('./devices.json').toRx().subscribe(res => this.devices = res.json());
        }
    }

    bootstrap(App,[HTTP_BINDINGS]);

这是一个演示 angular2 http 功能的存储库,可能会派上用场:

https://github.com/kaalpurush/ng2-http