Angular 2 - Http provider error -
i'm starting angular 2 , i'm trying http request.
the code follows
import { component, view, bootstrap } 'angular2/angular2'; import { todoinput } './todoinput.ts'; import { todolist } './todolist.ts'; import { todoservice } './todoservice.ts'; import { httpservice } './httpservice.ts'; @component({ selector: 'my-app' }) @ view({ directives: [todoinput, todolist] templateurl: 'templates/app.html' }) class appcomponent { color: string; numbr: number; hidden: boolean; constructor(public myservice: todoservice, public https: httpservice) { this.color = 'blue'; this.numbr = 5; this.hidden = false; https.gethttpdata(); } } bootstrap(appcomponent, [todoservice, httpservice]);
and http service follows:
import { http, http_providers } 'angular2/http'; import {injectable} 'angular2/core' @injectable() export class httpservice { private url: string = 'http://www.omdbapi.com/?s=walking'; data: array[string] = []; logerror(error):void { console.log(error); } constructor(public http: http) { } public gethttpdata() { this.http.get(this.url) .map(res => res.json()) .subscribe( data => this.data = data, err => this.logerror(err), () => console.log('random quote complete') ); } }
when ran app, following error: exception: no provider e! (appcomponent -> httpservice -> e)
, angular2.dev.js:21835 error: di exception
any ideas? missing?
thanks in advance help.
update managed make work. hope it's correct, works:
import { component, view, ngfor, form_directives, bootstrap } 'angular2/angular2'; import { todoinput } './todoinput.ts'; import { todolist } './todolist.ts'; import { todoservice } './todoservice.ts'; import { httpservice } './httpservice.ts'; import { http_providers } 'angular2/http'; @component({ selector: 'my-app' }) @view({ directives: [todoinput, todolist, ngfor, form_directives] templateurl: 'templates/app.html' }) class appcomponent { color: string; numbr: number; hidden: boolean; shows: [string] = []; constructor(public myservice: todoservice, public https: httpservice) { this.color = 'blue'; this.numbr = 5; this.hidden = false; } searchshows():void { this.https.gethttpdata(this.keyword).subscribe( data => this.shows = data.search, err => console.log(err), () => console.log('done!') ); } } bootstrap(appcomponent, [http_providers, todoservice, httpservice]);
and service class:
import { http } 'angular2/http'; import { inject } 'angular2/core' export class httpservice { private url: string = 'http://www.omdbapi.com/?s='; data: [string] = []; logerror(error):void { console.log(error); } constructor(@inject(http) public http: http) { } public gethttpdata(keyword) { return this.http.get(this.url + keyword) .map(res => res.json()); } }
i can't, however, make work service class uses http needed dependencies. seems main class has bootstrap http_providers. not sure why.
i don't use angular 2, but
- are sure http_providers inside import? http isn't enough?
- in provided code have whitespace between "@" , "view" :)
Comments
Post a Comment