primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Cannot close stacked dynamic dialog from DynamicDialogRef.close() and escape key closes all dynamic dialogs

Open cuneytcalishkan opened this issue 7 years ago • 14 comments

I'm submitting a ...

[X] bug report => Search github for a similar issue or PR before submitting

Plunkr Case (Bug Reports) Demo: https://github-fxeygh.stackblitz.io/ Editor: https://stackblitz.com/edit/github-fxeygh

Current behavior If you open inner dynamic dialogs via DialogService, firstly opened dialog cannot be closed programmatically calling by reference.close() method.

Expected behavior All stacking dialogs should be programmatically closable

Minimal reproduction of the problem with instructions Steps to reproduce the problem are available on the demo https://github-fxeygh.stackblitz.io/

  • Angular version: 7.1.4

  • PrimeNG version: 7.0.3

  • Browser: Chrome 71.0.3578.98 Firefox 64.0

  • Language: TypeScript 3.1.6

cuneytcalishkan avatar Jan 04 '19 16:01 cuneytcalishkan

DialogService by design one Dialog so for nesting, inner components needs to have its own service injected.

cagataycivici avatar Jan 23 '19 08:01 cagataycivici

Thanks a lot for the answer, adding the DialogService into providers of the inner component solved the problem. Cheers

cuneytcalishkan avatar Jan 23 '19 10:01 cuneytcalishkan

Hello, cuneytcalishkan Could you please tell me more detail about it. I had same problem, so I hope your kind help.

Thanks

wanginternnect avatar Jul 30 '19 11:07 wanginternnect

Hello @wanginternnect,

you just need to add the DialogService into the list of providers of your component that you are using the dialog service. This will inject a new instance of the dialog service.

e.g. Component1 opens Popup1, and you need to open another popup, Popup2, from within Popup1. You just need to put the DialogService into the providers array of your Popup1 component.

cuneytcalishkan avatar Jul 30 '19 11:07 cuneytcalishkan

Hello, @cuneytcalishkan Thanks for your kind reply. I have created dialog with DialogService, and on this dialog, I have created another dialog with dialogservice. Second dialog is closed by this.dynamicDialogRef.close(); But first dialog is not closed by this.dynamicDialogRef.close();- it works nothing. If you have any solution, please help me. Sincerely.

wanginternnect avatar Jul 30 '19 11:07 wanginternnect

@wanginternnect just check the stackblitz I provided in the description for the code sample. Current version is actually working because the DialogComponent has its providers: [DialogService] which injects its own instance.

You just have to add " providers: [DialogService] " to your "another dialog" and it should be fine

cuneytcalishkan avatar Jul 30 '19 12:07 cuneytcalishkan

Hello, @cuneytcalishkan Thanks again. I have added provider, but it is not working. Could you please show me source code of stackblitz demo?

Sincerely

wanginternnect avatar Jul 30 '19 13:07 wanginternnect

It is on the editor link up in the description, you should be able to access it: https://stackblitz.com/edit/github-fxeygh

cuneytcalishkan avatar Jul 30 '19 13:07 cuneytcalishkan

Hi, @cuneytcalishkan Thank you very much. You have posted great article here. I highly recommend you. I hope to connect with you for a long time. Thanks again.

wanginternnect avatar Jul 30 '19 13:07 wanginternnect

This will not work if I try to subscribe to the close event of both the dialogues.

alwinaugustin avatar Nov 24 '20 12:11 alwinaugustin

I am still having this issue, in my scenario, my application has a few modal openings from a couple of places in the application so I put those dialogs into my header component and opened them using data services, I have 2 dialogs named service-dialog and item-dialog both are opening from the header component. service-dialog and item-dialog has their own providers: [DialogService], in there component please help me here

@Component({ selector: 'app-header', templateUrl: './header.component.html', styleUrls: ['./header.component.scss'], providers: [DialogService], }) export class HeaderComponent implements OnInit, OnDestroy { openServiceDialog() { this.dialogService.open(ServiceDialog, { }); }

  openItemDialog() {
        this.dialogService.open(ItemDialog, { });
  }

}

Seerat-Ahmed avatar Aug 15 '22 09:08 Seerat-Ahmed

Hi, I landed here because DynamicDialogRef.close() didn't work for me at all, even with a single popup. I've achieved to get it work thanks to this discussion but it is misleading in the documentation. The ProductListDemo in the documentation that is inserted into DynamicDialog has DynamicDialgRef in providers:

@Component({
    selector: 'product-list-demo',
    templateUrl: './productlistdemo.html',
    providers: [DynamicDialogRef]
})
export class ProductListDemo implements OnInit {

However, there should be the DialogService put in provider array, not the DynamicDialgRef. I suggest changing it in the documentation (v16.2.0)

gil-ghalad avatar Aug 28 '23 07:08 gil-ghalad

Hi, I landed here because DynamicDialogRef.close() didn't work for me at all, even with a single popup. I've achieved to get it work thanks to this discussion but it is misleading in the documentation. The ProductListDemo in the documentation that is inserted into DynamicDialog has DynamicDialgRef in providers:

@Component({
    selector: 'product-list-demo',
    templateUrl: './productlistdemo.html',
    providers: [DynamicDialogRef]
})
export class ProductListDemo implements OnInit {

However, there should be the DialogService put in provider array, not the DynamicDialgRef. I suggest changing it in the documentation (v16.2.0)

Indeed, the documentation is misleading, it should be the DialogService put in provider array instead of DynamicDialgRef.

andromida avatar Nov 23 '23 16:11 andromida

For those having trouble with this, you don't need to add DynamicDialogRef or DialogService, if you do that you'll be adding a new and different instance to the one you already have. Don't add it to the providers array, just inject it into the constructor and you're done. Works like a charm!

nacholangdon avatar Jan 23 '25 19:01 nacholangdon