Haoliang Yu
Haoliang Yu

Reputation: 3097

Is angular/http at angular2.js or http.js?

This is a stupid question but I just don't see a concrete answer elsewhere.

I am using angular2@^2.0.0-beta.0 installed with npm. In the node_modules/angular2/bundles/ folder, if I want to use the Http service, should I include the angular2.js with http.js or just angular2.js alone?

Upvotes: 1

Views: 2231

Answers (2)

Eggy
Eggy

Reputation: 4174

Yes, you should include http.js or http.dev.js or http.min.js in addition to angular 2 core.

Upvotes: 1

Thierry Templier
Thierry Templier

Reputation: 202216

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

Upvotes: 5

Related Questions