Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2+ detect within a component if a click listener is assigned

Tags:

angular

I am creating a reusable component which is showing a rounded image thumbnail as content. I want to detect within this component if a developer has assigned a click listener to it and modify the style and behaviour of it slightly. Basically add a cursor: pointer style if a developer assigned a (click)=anyClickCallbackFunction() on the component.

The question is, how can i read this from within the component?

like image 489
DArkO Avatar asked Dec 15 '25 17:12

DArkO


1 Answers

for doing that you need to have click output in your component although you are not using it, then check how many observers exists

export class MycompComponent implements OnInit {

  @Output() click : EventEmitter<any> = new EventEmitter<any>();

  hasHandler : boolean;

  ngOnInit() {
    this.hasHandler = this.click.observers.length > 0;
  }

}

see working example here

like image 115
Reza Avatar answered Dec 18 '25 03:12

Reza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!