Angular Material Dialog not closing after navigation

后端 未结 5 2603
醉酒成梦
醉酒成梦 2021-02-20 09:43

I\'m working on an application that shows an overview of entities in a datatable. Every entity has linked entities that are shown as \'xxx linked entities\' in a column. When th

5条回答
  •  长发绾君心
    2021-02-20 10:23

    When you navigate using [routerLink] directive or router.navigate() method of Angular's Router service within the MatDialog, the actual navigation happens. So we can just listen for router's changes and close the dialog.

    In your EntityDialogComponent implement ngOnInit as following:

    ngOnInit() {
       this._routerSubscription = this._router.events
         .pipe(
           filter((event: RouterEvent) => event instanceof NavigationStart),
           filter(() => !!this.dialogRef)
         )
         .subscribe(() => {
           this.dialogRef.close();
         });
    }
    

    The code above closes a dialogRef when NavigationStart event is fired and there is an instance of the dialogRef.

    P.S. Make sure that the _routerSubscription is unsunscribed.

提交回复
热议问题