如何在AngularJS 2.0中发出HTTP请求
How to make HTTP requests in AngularJS 2.0
我只是想知道如何在新的angularjs 2.0中发出http请求,以及如何将此服务挂钩到控制器。
谢谢。
截至目前,http模块/组件仍在开发中,尚未发布。在此期间,有两种选择:
- 获取(https://github.com/github/fetch)
- axios (https://github.com/mzabriskie/axios)
你可以在这里了解更多关于新的http服务的信息:
- https://github.com/jeffbcross/http-design
- https://docs.google.com/document/d/1DMacL7iwjSMPP0ytZfugpU4v0PWUK0BT6lhyaVEmlBQ/edit
我认为已经添加了基本支持。基于这个例子:
-
用httpInjectables从'angular2/http'//index.ts引导你的应用
-
将Http注入构造函数constructor(Http: Http)//http_comps
-
http.get('./people.json').map(res => res.json()).subscribe(people => this.people = people);
节选:
import {Component, View} from 'angular2/core';
import {Http, HTTP_BINDINGS} from 'angular2/http';
import {bootstrap} 'angular2/platform/browser';
import {CORE_DIRECTIVES,NgFor} from 'angular/common';
@Component({
selector: 'app'
})
@View({
templateUrl: 'devices.html',
directives: [CORE_DIRECTIVES,NgFor]
})
export class App {
devices: any;
constructor(http: Http) {
this.devices = [];
http.get('./devices.json').toRx().subscribe(res => this.devices = res.json());
}
}
bootstrap(App,[HTTP_BINDINGS]);
下面是演示angular2 http特性的repo,这些特性可能会派上用场:
https://github.com/kaalpurush/ng2-http相关文章:
- Meteor如何接收HTTP请求
- 在我的情况下,如何进行http请求
- 使用插件收听Firefox标签的http请求
- http请求使用html而不是json进行响应
- 在node-js路由中发出http请求
- 是否可以在没有HTTP请求或<输入>
- Acync JS HTTP请求通知请求
- PhantomJs在控制台中发出http请求时不会返回任何状态
- PHP处理来自一个页面的http请求,并将其显示在另一个页面上
- Firefox插件-如何发出http请求
- 验收测试主要针对传出的http请求
- 验证HTTP请求来自NFC标签上的点击
- 如何制作'http请求'在Node js中同步
- 为什么我在Cloud9中的http请求总是返回错误
- AngularJS Typeahead和$http请求限制
- HTTP请求使用cURL,但在浏览器中使用javascript时会得到CORS响应
- 如何使用Chrome扩展API获取网页的所有HTTP请求
- 将http请求中的数据传递给C#控制器
- 在初始化路由之前发送$http请求
- 从客户端捕获 HTTP 请求(包括 ajax)