在 AngularJS 中,我使用内置的 $http 服务发送请求。
我应该使用什么来向 Angular 中的服务器发送请求?我找不到任何涵盖该主题的文档。
最佳答案
编辑:有一个api preview对于 Angular 2 上的新 http 服务 website现在
有一个基本的http service目前在 Angular 2 中,但它现在非常简约。该软件处于 alpha 阶段,很可能会发生变化,因此您可能只想使用 fetch API , 实现你自己的 XMLHttpRequest ,或使用类似 jQuery 的库反而。目前,Angular 2 http api 与 fetch API 基本相同。 .鉴于fetch改变的可能性要小得多,我建议只使用它。
如果您想使用 Angular 2 服务,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);
}
}
关于javascript - Angular 替代 $http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29361027/