How to include Angular 2 HTTP bundle
Introduction
In this section, we introduce how to include the Angular 2 HTTP and use it in your project.
Step 1: Add the HTTP bundle to <head>
Angular 2 has a module bundle for working with XHR and JSON API calls from the client, named the HTTP. Angular provides the HTTP bundle as a separate resource.
To start using this bundle, the first thing you need to do is update your html file, for example index.html, to include the bundle script. To do this, simply add another tag in the <head>
tag that links to the path as the other angular 2 file.
<script src="/your_path/angular2/http.dev.js" type="text/javascript"><script>
The http.dev.js
script is delivered as part of the Angular package. So when you pull that down you have the HTTP bundle script. It's just a matter of including it based on the module loading strategy you are on. The HTTP module works with observables and RxJS. So you need to make sure that the RxJS
bundle is being included as well. You should include a script tag for Rx.js in <head>
<script src="/your_path/angular2/Rx.js" type="text/javascript"><script>
After that, your project is good to go.
Step 2: Include HTTP_PROVIDERS
in bootstrap()
call
The last thing to do to get start using Angular's HTTP code is to include HTTP_PROVIDERS
in the bootstrap()
call.
So for example in the main.ts
file, you need to add a new import statement and import HTTP_PROVIDERS
from angular2/http
import {HTTP_PROVIDERS} from 'angular2/http'
Then, add it to the providers array in the bootstrap()
call, this will make Angular class like HTTP available to the app from constructor injection.
bootstrap(AppComponent, [HTTP_PROVIDERS]);
Latest Post
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Combine JavaScript and CSS
- Inline Small JavaScript and CSS
- Minify JavaScript and CSS
- Defer Parsing of JavaScript
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Percentile
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync