3

Just started out with angular 2, and encounter my first error. I am trying to create a service for fetching data from a server but am getting this error

Error: Can't resolve all parameters for CourseService: (?). at SyntaxError.ZoneAwareError (http:……}

this is the service code throwing the error app/courses/courses.service.ts

import { Http } from '@angular/http'; export class CourseService { private _courses : Object[]; public get courses() : Object[] { return this._courses; } public set courses(v : Object[]) { this._courses = v; } constructor(private http: Http) { http.get("https://jsonplaceholder.typicode.com/posts").subscribe(data => { this._courses = data.json(); }); } } 

this service is supposed to be used in app/courses/courses.component.ts

import { Component } from '@angular/core' import { CourseService } from "app/courses/course.service"; @Component({ selector: 'courses', providers: [CourseService], templateUrl: './courses.component.html', styleUrls: ['./courses.component.css'] }) export class CoursesComponent { title : string = "The title of the Course"; courses : Object[]; constructor(private courseService: CourseService) { console.log("hello"); this.courses = courseService.courses; } } 

and app.module contains

.... .... import { AppComponent } from './app.component'; import { CoursesComponent } from './courses/courses.component'; import { CourseService } from "./courses/course.service"; @NgModule({ declarations: [ AppComponent, CoursesComponent, ], imports: [ BrowserModule, FormsModule, HttpModule ], providers: [CourseService], bootstrap: [AppComponent, CoursesComponent] }) export class AppModule { } 
1
  • 1
    update the post with your import statements for this service accross your application?(all components and services if any) Commented Aug 4, 2017 at 23:09

1 Answer 1

6

put @Injectable() before export class .
@Injectable() lets Angular know that a class can be used with the dependency injector.

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

Comments