Nick Alexander
Nick Alexander

Reputation: 361

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"];
    }
}

Upvotes: 6

Views: 15570

Answers (3)

DavGarcia
DavGarcia

Reputation: 18792

I had the same inexplicable problem. I copy and pasted the code from the web and something must not have been right in the function declaration. I deleted the line

getCourses(): string[] {

and typed it out again by hand. When I ran the code, it worked. Maybe some invisible character was pasted in and messing up Typescript? Who knows.

Upvotes: 1

Diego
Diego

Reputation: 304

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

Upvotes: 2

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657068

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

 this.courses = courseService.getCourses();
                ^ lower case c

Upvotes: 7

Related Questions