How to check which component/service is calling a method in a service in angular 2,4?
Asked Answered
C

4

7

I want to check which component/service is calling the method of a certain service, without passing additional parameters. This needs to be checked in the service. Please mention if there's any way that this could be done.

Carvel answered 10/1, 2018 at 13:18 Comment(2)
Possible duplicate of Angular 2: Detect from which component service is calledGilder
@Baconbeastnz I don't want to pass parameters...Carvel
J
3

you could use console.trace() inside the service to find out which component is calling that service.

Jasminejason answered 29/7, 2020 at 5:15 Comment(0)
B
0

You could do it passing parameters to the called method. But i think this is your without passing additional variables..

How about event listener? your service could start an Event and you can check this Event in your multiple callers

EDIT:

the callee (who launch the event, maybe your service)

@Component({
  selector: 'yourComponent',
  templateUrl: './yourComponent.component.html',
  styleUrls: ['./yourComponent.component.scss']
})

export class yourClass extends OnInit {

@Output() theEvent = new EventEmitter();

  constructor() {}

  ngOnInit() {
  ....
  }

  eventLauncher() {
    this.theEvent.emit();
  }
  
}
<yourHTMLelement (click)="eventLauncher()"></yourHTMLelement>

Who read the caller of event: (in my case, appComponent, so everytime i'll wait for an event, in every page of my application):

export class AppComponent implements OnInit {

@ViewChild('referToElementCaller') theCaller: ElementRef;

.....

funct() {
  // the caller is 'this.theCaller'
  doSomething();
}
<yourComponent #referToElementCaller (theEvent)="funct()"></yourComponent>
Bargeboard answered 10/1, 2018 at 13:33 Comment(1)
Hi, can you be more specific about the event?Carvel
T
0

Most straight forward solution is to pass a parameter to the method in service.

Or you can use the pub-sub technique here. You can have some kind of global variable where u can add a entry everytime you call the service method from the component and then read that in the method and clear the value after doing so.

Teleology answered 10/1, 2018 at 14:10 Comment(2)
The thing is, I've got a lot of calls in my application, editing all those is a nightmare, that's why I was trying to figure out whether there is a way to detect it from the service without editing the component it's called..Carvel
That is technically not possible because the service would not know which component has initiated the method call unless the component specifies it. May be you could try out the stack trace option. If you can get hold of the stacktrace you can get the component name.Teleology
P
0

This can not be achieved without passing a parameter because it defies the definition/purpose of a Service.

The relationship between services is minimized to the level that they are only aware of their existence.

The consumer/caller/component/service only knows about the existence of the service whereas, the called service knows nothing about who the callee is?

Read detailed information about Services here - https://en.wikipedia.org/wiki/Service-oriented_architecture

Proficiency answered 18/6, 2020 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.