2

I am building an app using angular2/Typescript, and attempting to access a service injected as a dependency. In Angular 1.x, I would have accessed this service through the controller function, and all child functions would have been able to access the same dependency. Here, I am injecting it through the constructor of my component as follows:

   constructor( @Inject(JRummy) rummyGame: JRummy) {

        this.currentGame = new Game();
        rummyGame.startGame(this.currentGame);

    }

where rummyGame is the service I am attempting to inject. This is working correctly. However, after I have accessed this service within the constructor, how do I access it in other functions? For example, I have the following click function within the component, where I want to call a method in the rummyGame service:

  simulateComputer():void {

        rummyGame.computerPlaySolo();

    }

how does the rummyGame service get injected into this click function if it was just injected into the constructor? Do I need to create a property on the component to store the instance of that service after it has been accessed by the constructor?

2 Answers 2

3

Shorter way would be do declare variable as private so that it will be available in context(this) of class directly.

constructor( @Inject(JRummy) private rummyGame: JRummy) {

   //code here

}
//do access service object by having this.rummyGame
simulateComputer():void {
    this.rummyGame.computerPlaySolo();
}

Other thing is you need to have @Injectable decorator over you service class to make it injectable inside other component constructor as a dependency.

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

1 Comment

I tried this -the call to rummyGame.computerPlaySolo(); works correctly in the constructor with the signature constructor( @Inject(JRummy) private rummyGame: JRummy), however if i add the same method call in the simulateComputer function,I got the error:ORIGINAL EXCEPTION: TypeError: l_context.computerPlaySolo is not a function angular2.dev.js:22367 ORIGINAL STACKTRACE: angular2.dev.js:22367 TypeError: l_context.computerPlaySolo is not a function at AbstractChangeDetector.ChangeDetector_AppComponent_comp_0.handleEventInternal`
1

Discoved the issue -- The response from Pankaj is correct, but I didn't add the @Injectable() decorator onto the the rummyGame.computerPlaySolo() method in the service itself. It seems that when the method was called directly from the constructor using @Inject declaration in the constructor signature, it worked without the @Injectable() decorator, but when I attempted to access it in the click function, it couldn't access the function without the @Injectable declaration

Comments

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.