I have a notification component that is displayed as a child component in the parent component after a button click. It automatically hides after a certain number of seconds. Here is the code I have developed:
Parent component:
<button (click)="handleClick()">Initiate Message</button>
<app-notification [message]="message" [duration]="3000" [show]="show"></app-notification>
<app-notification [message]="message" [duration]="6000" [show]="show"></app-notification>
export class AppComponent {
message:string;
show:boolean;
handleClick(){
this.message = 'Good Morning';
this.show=true;
}
}
Child component:
<div class="container" *ngIf="show">
{{ message }}
</div>
@Input() message: string;
@Input() duration: number;
@Input() show = false;
constructor() {}
ngOnChanges(changes: SimpleChanges) {
console.log(this.show)
setTimeout(()=>{
this.show = false;
console.log(3)
}, this.duration)
}
The functionality works well, but I am facing an issue where if the initial notification has not finished hiding after 3 seconds and I click the button again, the new notification does not display. Can someone help me identify what I am doing wrong? I am still learning Angular.
Additionally, I would like to achieve the same behavior for both instances of the child component.