Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
How to trigger the method on click of custom link in angular 8

I have a scenario where I need to create a tag from TS and i need to capture the click event if someone click on tag

In Html

<p [innerHtml]="textField"></p>

<button (click)="generateLink()">click</button>

In Ts

textField = 'Hello click button for link';

  generateLink() {
    this.textField = `${this.textField} <a href="#" onclick="update()">click me</a>`;
  }

  update(){
    console.log('click me is clicked')
  }

Here whenever click me is click I need to trigger update method. link to stackblitz link

Please anyone let me know how to achieve it.Thanks

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

It feels that in this case using *ngIf would be the right idea to use here. In angular it does not compile HTML on the fly, like how angularjs used to do using $compile API.

What I mean is innerHTML tag will just do the work of rendering the HTML inside DOM, it won't enable any binding there (you can not call update method there). Hence you can use *ngIf directive to show and hide DOM conditionally.

<ng-container *ngIf="showLink">
  {{textField}}
  <a href="#" (click)="update()">click me</a>
</ng-container>

<button (click)="generateLink()">click</button>

TS

  showLink = false;

  generateLink() {
    this.showLink = true;
  }

Forked Stackblitz


An alternative way to generate link without using *ngIf, is by using combination of ng-template and ng-container to generate and inject template manually.

HTML

<ng-container #links></ng-container>

<button (click)="generateLinkFromTemplate()">another click</button>

<ng-template #link>
  {{ textField }}
  <a href="#" (click)="update()">click me</a>
</ng-template>

TS

@ViewChild('links', { read: ViewContainerRef }) links: ViewContainerRef;
@ViewChild('link', { read: TemplateRef }) link: TemplateRef<any>;

generateLinkFromTemplate() {
  this.links.createEmbeddedView(this.link);
}

Another Forked Stackblitz

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda