I have a very strange problem (never seen) with my component. I am trying to delete a row by clicking on a button inside the ngFor list. When I have only one line, it works, but when it contains more than one line, the event is fired twice, once for a good line and once for the first line (after deleting another line):
<label> <div class="tag" *ngFor="let o of selectedOptions;"> <span class="tag-text">{{ o.textContent }}</span> <button (click)="removeTag(o)" type="button" class="fa fa-times"></button> </div> </label>
And here is my witch method called twice (only if there is another option):
public removeTag (option: NxtSelectOptionComponent) { this.selectedOptions = [ ...this.selectedOptions.filter(o => o !== option), ] }
I tried to use splice, I tried to add stopPropagation ... I do not understand that I did this a lot of time, and this is the first time I see it.
EDIT: the removeTag method is called when I click on the .tag
element, so when I click on the button, it is called twice, but I canβt understand why ... the attribute (click)
only on the button
The problem is resolved: I found the problem ... The FYI shortcut label clicks on the button, so if you have any (click) attribute, it will fire twice.
source share