Angular Material Dialog not closing after navigation

后端 未结 5 2602
醉酒成梦
醉酒成梦 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:11

    There were no errors or warnings but closeOnNavigation: true started working for me only after adding these providers to the module with the dialog:

    import { Location, LocationStrategy, PathLocationStrategy, APP_BASE_HREF } from "@angular/common";
    
    ...
        providers: [
            Location,
            { provide: LocationStrategy, useClass: PathLocationStrategy },
            { provide: APP_BASE_HREF, useValue: '/' }
        ]
    ...
    

    I suspect that in my case this was because of using UI-Router and never initializing the built in Angular router before.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-20 10:23

    you can use this matDialogClose attribute.

    <button mat-button matDialogClose>Cancel</button>
    
    0 讨论(0)
  • 2021-02-20 10:34

    Hi according to the docs it says: closeOnNavigation: Whether the dialog should close when the user goes backwards/forwards in history.

    History Navigation Normally means browser back and forward buttons: https://developer.mozilla.org/en-US/docs/Web/API/History_API

    What you could do in your scenario however is instead of routing directly from your html is create a function that will route after the dialog is closed this way you can ensure that you close the dialog first before initiating your navigation.

       <a class="m-link m--font-bold" (mousedown)="navigateToEntity($event)">{{linkedEntity.name}}</a>
    

    Inside your component have something like

      navigateToEntity(event) {
        this.dialogRef.afterClosed.pipe(
          tap(() => this.router.navigate(['navigate to wherever'])),
          first()
        ).subscribe();
        this.dialogRef.close();
      }
    

    Hope this helps, i haven't worked much with Material Components but reading through some of the docs this is how i would implement it.

    0 讨论(0)
  • 2021-02-20 10:35

    I had this problem and I just fixed it now.

    you need to subscribe router events after creating the dialogRef object. you can check the code

    const dialogRef = this.dialog.open(MyComponent, {
      width: '380px',
      height: '320px',
    });
    this.router.events
     .subscribe(() => {
       dialogRef.close();
     });
    
    0 讨论(0)
提交回复
热议问题