Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: is not a function but it exist

I am trying to get an array out of a different class but he says the function does not exist. here is my code:

courses.component.ts:

import {Component} from 'angular2/core'
import {CourseService} from './course.service'

@Component({
    selector: 'courses',
    template: `
        <h2>Courses</h2>
        {{ title }}
        <ul>
            <li *ngFor ="#course of courses">
            {{course}}
            </li>
        </ul>
        `,
    providers: [CourseService]
})
export class CoursesComponent{
    title = "The title of courses page";
    courses;

    constructor(courseService: CourseService){
        this.courses = CourseService.getCourses();
    }
}

course.service.ts:

export class CourseService{
    getCourses() : string[]{
        return ["Course1","Course2","Course3"];
    }
}
like image 740
Nick Alexander Avatar asked Nov 20 '16 20:11

Nick Alexander


2 Answers

You need to reference the argument name, not the argument type

 this.courses = courseService.getCourses();
                ^ lower case c
like image 100
Günter Zöchbauer Avatar answered Nov 01 '22 02:11

Günter Zöchbauer


I think it's some kind of bug, because TypeScript recognize me the method but when I delete and type the method again getCourses() in the component, it says that the method is not found, then I go to the Service and start to delete blank lines, and the method Works. I'm currently using Angular 4

like image 22
Diego Avatar answered Nov 01 '22 03:11

Diego