0

I am following Angular's own main tutorial on setting up a Javascript project. This works fine, but it stops after 'hello world' and continues with Typescript.

Most typescript examples can be translated back to javascript and still work, but now I am stuck at making HTTP calls.

In index.html I load all modules as separate script files:

    <script src="node_modules/@angular/http/bundles/http.umd.js"></script>

In app.module.js I load ng.http.HttpModule and according to most answers here on stackoverflow, I need to include ng.http.HTTP_PROVIDERS in the bootstrap part:

(function(app) {

  app.AppModule =
    ng.core.NgModule({
      imports: [ ng.platformBrowser.BrowserModule, ng.forms.FormsModule, ng.http.HttpModule ],
      declarations: [ app.AppComponent, app.Actorlist ],
      bootstrap: [ app.AppComponent, [ng.http.HTTP_PROVIDERS] ]
    })
    .Class({
      constructor: function() {}
    });

})(window.app || (window.app = {}));

The HTTP_PROVIDERS part generates this error:

Unexpected value 'undefined' used in the bootstrap property of module 'class2'

If I leave that out I don't get an error, but I still can't call http from my component:

(function(app) {

  app.Actorlist =
    ng.core.Component({
      selector: 'actor-list',
      templateUrl: './app/actorlist.component.html'
    })
    .Class({
      constructor: function() {
      },

      searchActor: function(){
        var result = this.getActor();
        console.log(result);
      },

      getActor: function(){
        return ng.http.get(this.url)              
                .map(function (response) {
                    return response.json().data;
                })
                .catch();
      }
    });

})(window.app || (window.app = {}));

This results in the error:

 Error in app/actorlist.component.html:2:0 caused by: ng.http.get is not a function

How do I get the http reference into my component?

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
Kokodoko
  • 26,167
  • 33
  • 120
  • 197
  • I have to add, just using window.fetch instead of http.get (fetch is already supported in chrome and firefox) solves a whole big world of problems... – Kokodoko Nov 22 '16 at 22:41
  • They are apples and oranges. `http` isn't glorified `fetch` and isn't available as global variable. You should use [dependency injection](http://stackoverflow.com/a/39029435/3731501) to get `http` instance inside `Actorlist` class. – Estus Flask Nov 22 '16 at 23:04
  • Why are you bootstrapping http providers? – silentsod Nov 23 '16 at 00:29
  • I have no idea how to get the http functionality in my component, that's why I posted this question :) This seems to be a work in progress in the official docs :). I know fetch is different, but it seems to solve a complex problem in a simple way. – Kokodoko Nov 23 '16 at 12:47
  • I am unfamiliar with the constructor injection syntax in JS but after some searching, in your app constructor, try something like `constructor: [Http, function(http) { <...>}]` If that works I'll make an answer out of it. And you're right, information is a little sparse for this in JS. – silentsod Nov 23 '16 at 15:46

0 Answers0