我在父组件中有一个HTML模板用于警告模式,如
<parent>
<div class="alert" *ngIf="autoCloseAlert">
<div class="alert__message">
<ng-content></ng-content>
<div class="alert__close icon-close" (click)="close()"></div>
</div>
</div>
</parent>
当父组件中发生某些CRUD操作时,我正在显示警报,5秒后我自动关闭警报
export class CmaAlertComponent implements OnInit{
public autoCloseAlert: boolean = true;
public ngOnInit(): void {
setTimeout(() => {
this.autoCloseAlert = false;
}, 5000);
}
}
问题是警报仅在第一次显示,之后没有显示,因为在ngOnInit()中autoCloseAlert设置为false。如何在setTimeOut()完成后将autoCloseAlert设置回true?