Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrimeNg p-dialog not being centered on first display

I'm building an angular 2 application, and am building a dialog modal form, using PrimeNg p-dialog. I'm noticing that in some instances, the dialog window is being displayed at the far left of the screen (rather than being centered). However, on dragging the browser window, the dialog gets centered correctly.

I'm using a very simple dialog (as per primeng documentation), and afterwards setting display to true:

<p-dialog header="Title" [(visible)]="display">
    Content
</p-dialog>

Any ideas why the dialog is not being centered from the beginning?

like image 878
user2707674 Avatar asked Oct 26 '25 16:10

user2707674


2 Answers

This worked for me:

From the component I get a reference to the dialog like so:

import { Dialog } from 'primeng/dialog';
...
@ViewChild(Dialog) dialog;

And in the function that causes the dialog to open I call the center method in a setTimeout.

window.setTimeout(() => { this.dialog.center(); });
like image 187
stephan.peters Avatar answered Oct 29 '25 05:10

stephan.peters


After investigating further I found out that the issue was that the dialog was wrapped around in an ngIf. After removing that, the positioning was corrected.

like image 40
user2707674 Avatar answered Oct 29 '25 07:10

user2707674



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!