Angular 替代 $http

Angular alternative $http

在用于发送请求的 AngularJS 中,我使用内置的 $http 服务。

我应该使用什么向 Angular 中的服务器发送请求?我找不到涵盖该主题的任何文档。

编辑:现在有一个api preview for the new http service on the Angular 2 website

有一个基本的 http service in Angular 2 currently, but its very minimalist right now. This software is in alpha and is very likely to change, so you may just want to use the fetch API, implement your own XMLHttpRequest, or use a library like jQuery instead. Currently, the Angular 2 http api is essentially identical to the fetch API. Given that fetch 不太可能改变,我建议只使用那个。

如果你想使用Angular2服务,here就是一个例子...

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);
  }
}

简单的 http 获取示例:

http.get('./friends.json').map((res: Response) => res.json()).subscribe(res => this.result = res);

我这里有一个工作示例:http://www.syntaxsuccess.com/viewarticle/angular-2.0-and-http