raf
raf

Reputation: 195

How to trigger click event on angular 5 with Jquery

I want to trigger click event based on each element id but it is not working.

Here is my code:

ngOnInit() {
  this.getProductsLists();
}

getProductsLists() {
  this.supplierService.getProductLists()
    .subscribe(data => {
      this.productData = data;
      this.productData.forEach((value) => {
        value.prodCategoryChild.forEach((element) => {
          $('#prod' + element.id).click(() => {
            alert('This is not working');
          })
        });
      });
    });
}

What am I missing here? Thanks.

Upvotes: 1

Views: 1973

Answers (1)

Vanojx1
Vanojx1

Reputation: 5574

An Angular way should be like this:

<div-or-whatever (click)="clickHandler(prod.id)" *ngFor="let prod of productData" >{{ prod.label }}</div-or-whatever>

then

clickHandler(id) {
    alert(`Clicked prod ${id}`);
}

Upvotes: 1

Related Questions