繁体   English   中英

Angular替代$ http

[英]Angular alternative $http

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

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

编辑 :现在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);
  }
}

简单的http get样本:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM