<ul> <li *ngFor="let i of items"> <p>{{i.name}}</p> <button mat-button *ngIf="func();" (click)="editMessage()">edit</button> </li> </ul> 

If you push a button outside the loop, only the editMessage () function is triggered. Inside - 0 reactions

component:

 import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-chat', templateUrl: './chat.component.html', styleUrls: ['./chat.component.scss'] }) export class ChatComponent implements OnInit { func() { return true; } editMessage() { console.log('asd'); } constructor() { } ngOnInit() { } } 
  • no quotes after *ngIf - Vitaly Shebanits
  • This is a typo in the example, everything is OK in the code - Danil
  • everything is ok .. maybe this is not the problem stackblitz.com/edit/angular-angular-likjsd8 - Vitaly Shebanits
  • It really works that way. I only have <li * ngFor = "let i of items ()"> and the items () function itself returns an array with objects from localStorage. In fact, there is the same thing if you just write with your hands as in your example. that's just click does not work - Danil
  • Possible duplicate question: The click event does not work inside the ngFor loop - Hooter

0