Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject Http in Angular 2 without typescript

The examples given by the angular team only show how to inject Http for typescript.

https://angular.io/docs/js/latest/api/http/Http-class.html

What's the JS equivalent of this:

import {Http, HTTP_PROVIDERS} from 'angular2/http';

and this:

constructor(http: Http) { }
like image 793
HankScorpio Avatar asked Dec 02 '25 09:12

HankScorpio


1 Answers

Assuming you're at alpha 49 or newer (you should be at least at beta.0) and you're using the UMD bundles the correct answer is to use ng.http.Http and ng.http.HTTP_PROVIDERS

var App = ng.core.
          Class({
            constructor: [ng.http.Http, function(http) {
              /* Do something with http */
            }]
          });

document.addEventListener('DOMContentLoaded', function() {
  ng.platform.browser.bootstrap(App, [ng.http.HTTP_PROVIDERS]);
});

Here's a plnkr with a working example.

like image 94
Eric Martinez Avatar answered Dec 03 '25 21:12

Eric Martinez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!