Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: Detect from which component service is called

Tags:

angular

I have MyService which is injected into ComponetA and ComponetB.

Is it possible to find out from which component is the MyService.myMethod() called?

@Injectable()
export class MyService {

  public myMethod(): void {
    console.log('called from component: ' + component);
  }
}
like image 613
Tomas Marik Avatar asked Oct 29 '22 10:10

Tomas Marik


1 Answers

Only solution would be to use a parameter which would be passed to the called function.

EDIT: Previous suggestion to refer here would not work because it only works without strict mode.

like image 74
Mantas Avatar answered Nov 15 '22 05:11

Mantas