I\'ve been playing with Angular 2 Quickstart.
How can I use/import http module in Angular 2?
I\'ve looked at Angular 2 Todo\'s.js, but it doesn\'t use the ht
Its already in angular2, so you dont need to put anything in package.json
You just have to import and inject it like this. (this is a Stuff service with a methodThatUsesHttp() that just logs the response)
import {XHR} from 'angular2/src/core/compiler/xhr/xhr';
export class Stuff {
$http;
constructor($http: XHR) {
this.$http = $http;
}
methodThatUsesHttp() {
var url = 'http://www.json-generator.com/api/json/get/cfgqzSXcVu?indent=2';
this.$http.get(url).then(function(res) {
console.log(res);
}, function(err) {
console.log(err);
});
}
}
For Angular 4.3+, 5.+
// app.module.ts:
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
// Import HttpClientModule from @angular/common/http
import {HttpClientModule} from '@angular/common/http';
@NgModule({
imports: [
BrowserModule,
// Include it under 'imports' in your application module
// after BrowserModule.
HttpClientModule,
],
})
export class MyAppModule {}
And inside your service class
import { HttpClient } from '@angular/common/http';
Other packages you might also need
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
In package.json
"@angular/http": "^5.1.2",
Reference is here
Following up on a few of the answers, here is a complete working example of using the http
module
index.html
<html>
<head>
<title>Angular 2 QuickStart</title>
<script src="../node_modules/es6-shim/es6-shim.js"></script>
<script src="../node_modules/systemjs/dist/system.src.js"></script>
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="../node_modules/angular2/bundles/http.dev.js"></script>
<script>
System.config({
packages: {'app': {defaultExtension: 'js'}}
});
System.import('app/app');
</script>
</head>
<body>
<app>loading...</app>
</body>
</html>
app/app.ts
import {bootstrap, Component} from 'angular2/angular2';
import {Http, Headers, HTTP_PROVIDERS} from 'angular2/http';
@Component({
selector: 'app',
viewProviders: [HTTP_PROVIDERS],
template: `<button (click)="ajaxMe()">Make ajax</button>`
})
class AppComponent {
constructor(public http: Http) { }
ajaxMe() {
this.http.get('https://some-domain.com/api/json')
.map(res => res.json())
.subscribe(
data => this.testOutput = data,
err => console.log('foo'),
() => console.log('Got response from API', this.testOutput)
);
}
}
bootstrap(AppComponent, []);
import {Injectable} from 'angular2/core';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
@Injectable()
export class GroupSelfService {
items:Array<any>;
constructor(http:Http){
http.get('http://127.0.0.1:8080/src/data/names.json')
.subscribe(res => {
this.items = res;
console.log('results found');
})
}
}
Results in a 404:
File change detected
File change detected
GET /src/angular2/http 404 0.124 ms - 30
Two strange things:
1. /src/angular2/http - is not the path where http can be found and not the path I've provided in the code.
2. core.js lies just beside http.js in the node_modules/angular2 folder and is found.
How strange is that?
Update
Mea culpa: None of the examples mentioned that you need to reference the http.js in your html like
<script src="../node_modules/angular2/bundles/http.dev.js"></script>
...and then it worked.
But for the path in the error message I still have no explanation.
A simple example using the http module:
import {Component, View, bootstrap, bind, NgFor} from 'angular2/angular2';
import {Http, HTTP_BINDINGS} from 'angular2/http'
@Component({
selector: 'app'
})
@View({
templateUrl: 'devices.html',
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]);
In version 37 you need to do this:
///<reference path="typings/angular2/http.d.ts"/>
import {Http} from "angular2/http";
And run this tsd command:
tsd install angular2/http