I have an issue with a click event handler not firing when its containing div was originally hidden, then later displayed. The code below is Angular, but I am uncertain if this is an Angular issue.
ParentComponent.ts
@Component({
selector: "parent",
template: `
<div (click)="showChildren()">Click Here!</div>
<div class="container">
<ng-content>
</div>
`,
styles: [`
.container {
display: none;
}
.show .container {
display: block;
}
`]
})
export class ParentComponent {
@HostBinding("class.show") show = false;
showChildren(): void {
this.show = true;
}
}
ChildComponent.ts:
@Component({
selector: "child",
template: "<ng-content>"
})
export class ChildComponent.ts {
@HostListener("click")
clickMe(): void {
/* This event is never captured */
}
}
Usage:
<parent>
<child>Child 1</child>
<child>Child 2</child>
<child>Child 3</child>
</parent>
Rendered Output:
<parent>
<div (click)="showChildren()">Click Here!</div>
<div class="container">
<child (click)="clickMe()">Child 1</child>
<child (click)="clickMe()">Child 2</child>
<child (click)="clickMe()">Child 3</child>
</div>
</parent>
Here is what is happening:
- When the page first renders, the div with the "container" class is initially hidden because
displayis set tonone - Next, the user clicks on "Click Here!" and the class "show" is added to the parent div. This changes
displayon the container toblock.
Everything works properly up to here. The container div is rendered the child elements are displayed.
Here is the problem: The click handlers on the child elements do not fire. If the child elements are originally shown with display: block;, the click handlers work properly. It's only when the containing div is first hidden and later displayed that they do not work.
Any idea why? Any help is appreciated.