14

I'm starting learning Angular2 by following the quickstart they provide in their page, and now I'm trying to make some Http requests. But whenever I bootstrap the component, Chrome keeps giving me this errors:

Uncaught SyntaxError: Unexpected token <         http:1
Uncaught SyntaxError: Unexpected token <         angular2-polyfills.js:138
   Evaluating http://localhost:3000/angular2/http
   Error loading http://localhost:3000/app/boot.js

This is the component:

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

@Component({
  selector: 'users',
  providers: [HTTP_PROVIDERS],
  templateUrl: 'app/views/users.html'
})
export class UsersComponent {

  people: Object[];
  constructor(http: Http) {
    http.get('http://192.168.56.101/api/test').subscribe(res => {
      this.people = res.json();
      console.log(this.people);
    });
  }
}

And the bootstrapping:

import {bootstrap}    from 'angular2/platform/browser'
import {UsersComponent} from './components/users'

bootstrap(UsersComponent, [HTTP_PROVIDERS]);

The view is only {{people}}.

TypeScript is compiling OK. I don't even know what's failing!

1

3 Answers 3

26

The documentation is lacking on that part. Router and Http need to be added to the index.html. Easy mistake to make

1
  • 1
    quick note, the script tags are: <script src="node_modules/angular2/bundles/router.dev.js"></script> <script src="node_modules/angular2/bundles/http.dev.js"></script>
    – colbyJax
    May 13, 2016 at 13:17
7

First, if you injected provider on bootstrap, you don't have to do it again in component.

Second, did you included http.js in your index.html?

And third, you have an error in your code. There should be this.http.get() not http.get()

1

in bootstraping you do not have to import the HTTP_PROVIDERS dependencies. so try:-

import {HTTP_PROVIDERS}    from 'angular2/http';
import {bootstrap}    from 'angular2/platform/browser'
import {UsersComponent} from './components/users'

bootstrap(UsersComponent, [HTTP_PROVIDERS]);

and in index file you need to add :-

<script src="node_modules/angular2/bundles/router.dev.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.11/http.dev.js"></script>

for routing and http

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.