2

i am trying to access a simple method from a service class in a component.

//myfriend.service.ts

export class MyFriendService(){

constructor(){}

testMe() {
    window.alert("im friend service");
}
}


//myfriend.component.ts
import {Component} from 'angular2/core';
import {MyFriendService} from 'app/components/myfriend/myfriend.service';

@Component({
directives: [],
providers: [MyFriendService],
templateUrl: `app/components/myfriend/myfriend-view.html`
})

export class MyFriendCmp {

constructor(myFriendService: MyFriendService) {

    myFriendService.testMe();

}

}

it is throwing an error

myFriendService.testMe is not a function.

I spent a lot of time in this to get this work.but still trying.Help me to figure this out. Thanks!.

1 Answer 1

2

Working Plnkr

Oh can you believe this? It took very long to figure it out. But you have put

not required parenthesis look at this line export class MyFriendService() <=This parenthesis not required.

I have made two to three plunkr and every time it was working then took lot of time to figure this out that you have made problem with parenthesis.

export class MyFriendService{

constructor(){}

    testMe() {
         window.alert("im friend service");
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm trying to bring this in plunkr and will give you the link. What you mean by outside. I have imported it anyway.
@micronyks nice eyes +1..even I took 10 mins with plunkr & left it.. :D
It took so long @Pankaj. I was like everything is fine then what's the problem. Then line by line I compared everything and found out the problem. Then I updated answer but then lately came to know that around 22 mins ago Nishanthd solved it. hahahaha...
@micronyks great.Cheers now. :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.