1

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?

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.