To use the Http class in Angular2, you need to include the http.dev.js file:
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>
You need to specify HTTP_PROVIDERS when bootstrapping your main component to maker it available in dependency injection:
import {bootstrap} from 'angular2/platform/browser'
import { HTTP_PROVIDERS } from 'angular2/http';
(...)
bootstrap(MainComponent, [ HTTP_PROVIDERS ]);
You can then inject it within a component or a service as described below:
@Component({
selector: 'first-app',
template: `
(...)
`
})
export class AppComponent {
constructor(http:Http) {
this.http = http;
}
executeHttpRequest() {
this.http.get('https://angular2.apispark.net/v1/companies/')
.map(res => res.json())
.subscribe({
data => this.companies = data });
}
}
Hope it helps you,
Thierry