I have an angular app, where I have a popup component. I can control the popups visibility through it's parent and also from itself.
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<popup [visible]="visible"></popup>
<button (click)="onShow()">Show</button>
<button (click)="onHide()">Hide</button>
`,
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
public visible: boolean = false;
public onShow(): void {
this.visible = true;
}
public onHide(): void {
this.visible = false;
}
}
popup.component.ts
import { Component, Input } from '@angular/core';
@Component({
selector: 'popup',
template: `<div *ngIf="visible">My fancy popup <button (click)="onClick()">Click to close</button></div>`,
})
export class PopupComponent {
@Input()
public visible: boolean;
public onClick(): void {
this.visible = false;
}
}
Working stackblitz.
Use case:
- user can close the popup by clicking on button in popup
- user can close the popup by clicking on button in parent's component
- user can display the popup by clicking on button in parent's component
The problem:
- click to show the popup
- click inside popup to hide it
- click to show the popup
- the popup doesn't show up
As far as I know, its because the visible member inside app.component.ts doesn't changes, so the PopupComponent's @Input() value doesn't changes either. Any idea how to fix this?