Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update width or height of open Angular Material 2 dialog

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.

like image 735
bmd Avatar asked Feb 24 '18 09:02

bmd


People also ask

How to create a material dialog in angular?

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

How to update the width or height of an open dialog?

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.

How to integrate modal popup in angular 13 using Angular Material?

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

Why does matdialog need a componentfactory in angular?

Because MatDialog instantiates components at run-time, the Angular compiler needs extra information to create the necessary ComponentFactory for your dialog content component.


1 Answers

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;
    };
like image 54
bmd Avatar answered Nov 15 '22 11:11

bmd