17

I have created a service for Angular 2:

import {Injectable} from "angular2/core";
import {Http} from "angular2/http";
import {TaskModel} from "./tasks.model"

@Injectable()
export class TasksDataService {

  tasks:Array<any>;

  constructor(http:Http) {

    this.tasks = [
      new TaskModel("Dishes"),
      new TaskModel("Cleaning the garage"),
      new TaskModel("Mow the grass")
    ];
  }

  getTasks():Array<any> {
    return this.tasks;
  }
}

When I run my program the browser shows:

system.src.js:1049 GET http://localhost:3000/angular2/http.js 404 (Not Found)

I have looked up tutorials and they include angular2/http the same way. Does anyone see what is going wrong?

1 Answer 1

35

You need to check that you include the http.dev.js file in your main HTML 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/router.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>

You need then to add the HTTP_PROVIDERS as second parameter of the bootstrap function:

import { HTTP_PROVIDERS } from 'angular2/http';

bootstrap(MyMainComponent, [ HTTP_PROVIDERS ]);

Hope it helps you, Thierry

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

9 Comments

Thanks! But this triggered another error: angular.js:12477 Error: No provider for Http! (Tasks -> TasksDataService -> Http). I read about including it as a provider for a component, but this one is just a class. Any clues?
You need to add HTTP_PROVIDERS as second parameters of your bootstrap function ;-)
I have read that. But this is a Angular 1 hybrid app. So my bootstrap looks like: adapter.bootstrap(document.body, ["app"]); and adapter.bootstrap(document.body, ["app", HTTP_PROVIDERS]) did not work :)
Perhaps try to put it within an array: [ 'app', [ HTTP_PROVIDERS ] ]
Great thought but didn't work. However I will create a new question for this issue
|

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.