Is there a way to add (click) programatically?


#1

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.removeAttribute('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?

Thanks


#2

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.


#3

@rapropos So what could be solution in my case?


#4

Here is what I did with Markdown.


#5

Thanks. Will have a look at it.