Angular替代$ http

Eug*_*ene 12 javascript ajax angular-http angular

在AngularJS中发送请求我使用builtin $ http服务.

我将使用什么来向Angular中的服务器发送请求?我找不到任何涉及该主题的文档.

Cha*_*tin 5

编辑:现在Angular 2网站上有新的 http 服务的api 预览

目前Angular 2 中有一个基本的http 服务,但它现在非常简约。该软件处于 alpha 阶段,很可能会发生变化,因此您可能只想使用fetch API,实现您自己的XMLHttpRequest,或者改用像jQuery这样的库。目前,Angular 2 http api 与fetch API基本相同。鉴于fetch不太可能改变,我建议只使用它。

如果你想使用 Angular 2 服务,这里有一个例子......

import {bootstrap, Component, View, NgFor, Inject} from 'angular2/angular2';
import {Http, httpInjectables} from 'angular2/http';

@Component({selector: 'http-app'})
@View({
  directives: [NgFor],
  template: `
    <h1>people</h1>
    <ul class="people">
      <li *ng-for="#person of people">
        hello, {{person.name}}
      </li>
    </ul>
  `
})
export class HttpCmp {
  people: Object;
  constructor(http: Http) {
    http.get('./people.json').toRx().map(res => res.json()).subscribe(people => this.people = people);
  }
}
Run Code Online (Sandbox Code Playgroud)