5

My custom component's click() function is triggered twice - both custom component's event and sample level event are triggered.

Here's my Plunker.

0

5 Answers 5

23

Because you have bound it twice on the child component and on the parent component. The mouseEvent propagates from the child component to the parent component by default. You can stop propagation of event to parent component.

Template:

<div (click)="divClick($event)">Custom Div Clcik here!</div>

Class:

divClick(event) {
    event.stopPropagation();
    alert("divClick");
}
Sign up to request clarification or add additional context in comments.

Comments

2

Your problem is that you call click() event on your parent component and in your child component: here:

<cus-div (click)="onClick()"></cus-div>

and here:

<div (click)="divClick()">Custom Div Clcik here!</div>

remove the click event on your <cus-div></cus-div> of your click event and it will trigger once

Comments

1

i had a similar issue, i found the solution for mine, i had two *ngIf, one negated, to swap a icon in my case. I replaced *ngIf with [hidden] (display: none;).

Comments

0

Try:

event.preventDefault();

instead of:

event.stopPropagation();

Comments

0

my believe is: add emit solves it as well

Template

<div (click)="divClick($event)">Custom Div Clcik here!</div>

Class

@Output()
divClick = new EventEmitter<any>().emit;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.