How to make HTTP requests in AngularJS 2.0

时间:2015-05-18 18:03:23

标签: javascript http angular

I just want to know how i can make http requests in the new angularjs 2.0 and how to hook this service to a controller.

Thank you.

3 个答案:

答案 0 :(得分:5)

截至目前,http模块/组件仍在开发中,尚未发布。在此期间,有两种方法可供使用:

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

答案 1 :(得分:5)

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

  1. 使用来自'angular2 / http'//index.ts

  2. 的httpInjectables引导你的应用程序
  3. 在构造函数构造函数中注入Http(http:Http)//http_comp.ts

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

答案 2 :(得分:0)

以下是摘录:

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/gkopff/gson-javatime-serialisers