在用于发送请求的AngularJS中,我使用内置的$ http服务。
我应该用什么方式向Angular的服务器发送请求?我找不到涵盖该主题的任何文档。
在用于发送请求的AngularJS中,我使用内置的$ http服务。
我应该用什么方式向Angular的服务器发送请求?我找不到涵盖该主题的任何文档。
编辑:有一个对角2 website新的HTTP服务的api preview现在
有一个在目前2角一个基本http service,但它非常简约现在。此软件采用alpha版本,很可能会发生更改,因此您可能只想使用fetch API,实施自己的XMLHttpRequest,或者使用类似jQuery的库。目前,Angular 2 http api与fetch API基本相同。鉴于fetch更不可能改变,我建议只使用它。
如果你想使用的角度分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);
}
}
谢谢。好消息。 – Evgeniy 2015-06-23 04:46:23
任何想法,为什么这不工作了? 'map'不是'EventEmitter'的成员 – 2015-07-21 19:26:45
查看我在编辑中添加的链接中的“突破性更改”部分。基本上,你只需要在调用.map之前调用.toRx()即可 – 2015-07-27 01:30:30
普通的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
您仍然可以使用$ http。它不工作? – sms 2015-03-31 05:49:39
我使用AngularJS2而不是1.x.你有没有例子如何使用$ http? – Evgeniy 2015-03-31 06:06:06
您是否使用Angular 2的alpha?如果是这样,它还没有http服务。他们正在设计这里:https://github.com/jeffbcross/http-design – rob 2015-04-01 14:44:42