Is there a way to add (click) programatically?

Can we add (click)="somemethod()" programmatically?

I am trying to find all the a tags in a div and add the click function so that they would open in In App Browser.

@ViewChild('newsSingleContent') newsDiv: any;

 ngAfterViewInit() {
let child = this.newsDiv.nativeElement;
    let matches = child.querySelectorAll('a[href]');
    matches.forEach((obj) => {
      let link = obj.href;
      obj.setAttribute('click', 'launch('+link+')');

Not sure if it is the proper way to do so, but the problem is, setAttribute doesn’t take “(click)”. If this is not good practice, could you guide me in a proper path?


Directly manipulating the DOM in Angular apps has caused me no end of pain, so I do absolutely everything I can to avoid doing it at all.

1 Like

@rapropos So what could be solution in my case?

Here is what I did with Markdown.

Thanks. Will have a look at it.