Is there a method to change the width (or height) of an open Angular Material 2 dialog?
The closest I got was getting a reference to the dialog and calling the updateSize
method from within the dialog. Though I don't think the dialog reference method works in this way.
I use a dialog to capture and submit form data. Based on the response, success or error, the content within the dialog updates. It is at this point I would like to reduce the width of the dialog.
I have installed Angular v4.2.4
and Angular Material 2 v2.0.0-beta.12
Happy to provide additional information or code examples if needed. Or to consider a different approach.
Step 1:Install Angular Project Step 2:Add Angular Material Module Step 3:Build Material Modules File Step 4:Create Full Screen Modal Popup Step 5:Change Material Dialog Position Step 6:Complete Dialog Structure Step 7:Bind Callback Methods with Dialog Step 8:Run Angular Application Install Angular Project
To update the width or height of an open dialog, you get a reference to the open dialog and call the updateSize method. The first parameter sets the width and the second parameter sets the height. Both are of type string.
It grabs the user’s attention and forces them to take appropriate action without hurting the overall user experience. How to Integrate Modal Popup or Dialog in Angular 13 using Angular Material Step 1:Install Angular Project Step 2:Add Angular Material Module Step 3:Build Material Modules File Step 4:Create Full Screen Modal Popup
Because MatDialog instantiates components at run-time, the Angular compiler needs extra information to create the necessary ComponentFactory for your dialog content component.
I was on the right track. I just had the arguments wrong.
To update the width or height of an open dialog, you get a reference to the open dialog and call the updateSize
method. The first parameter sets the width and the second parameter sets the height. Both are of type string
.
import { Component, OnInit, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
@Component({
selector: 'app-my-dialog',
templateUrl: './my-dialog.component.html'
})
export class MyDialogComponent implements OnInit {
private shouldSizeUpdate: boolean;
constructor(private dialogRef: MatDialogRef<MyDialogComponent>,
@Inject(MAT_DIALOG_DATA) data: any) {
this.shouldSizeUpdate = data.shouldSizeUpdate;
}
ngOnInit() {
if (this.shouldSizeUpdate) this.updateSize();
}
updateSize() {
this.dialogRef.updateSize("300px", "500px");
}
}
And the code for further reference.
/**
* Updates the dialog's width and height.
* @param {?=} width New width of the dialog.
* @param {?=} height New height of the dialog.
* @return {?}
*/
MatDialogRef.prototype.updateSize = function (width, height) {
if (width === void 0) { width = 'auto'; }
if (height === void 0) { height = 'auto'; }
this._getPositionStrategy().width(width).height(height);
this._overlayRef.updatePosition();
return this;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With