Passing data from a component to Material Dialog

1k views Asked by At

I have been working around to pass the data from a component to Material dialog below is my code

First Component html file

<button md-raised-button (click)="openDialog()"><md-icon>add</md-icon</button>

In first component .ts file

data = {
customerid : 'abc',
appID : 'xyz',
description : 'this is looooooooooooooooooooooooooooooooooooooooonnnnnnnnnngggggggggggggg text'
 };
constructor(public dialog: MdDialog) { }

openDialog() {

    let dialogRef = this.dialog.open(AddEditAppDetailsComponent, {
                    width: '40%',
                    data: this.data,
                    disableClose: true,
    });

    dialogRef.afterClosed().subscribe( result => {
               console.log(`Dialog Closed: ${result}`);
               this.dialogResult = result;
    });

    dialogRef.updatePosition();
}

Material dialog html file

<form #f="ngForm" (ngSubmit)="onCloseConfirm(f.value)">
    <md-dialog-content>
        <md-grid-list cols="12" rowHeight="70px">
            <md-grid-tile [colspan]="6" [rowspan]="1">
                <md-input-container>
                    <input mdInput ngModel required #customerid=ngModel name="customerid" placeholder="Customer ID" value={{data.customerid}}>
                </md-input-container>
            </md-grid-tile>
            <md-grid-tile [colspan]="6" [rowspan]="1">
                <md-input-container>
                    <input mdInput name="appID" placeholder="App ID" value={{data.appID}}>
                </md-input-container>
            </md-grid-tile>
            <md-grid-tile [colspan]="12" [rowspan]="2">
                <md-input-container>
                    <textarea mdInput placeholder="Description" rows="5" value={{data.description}}></textarea>
                </md-input-container>
            </md-grid-tile>
        </md-grid-list>
    </md-dialog-content>
</form>
<md-dialog-actions>
    <button md-raised-button type="submit" class="saveBTN" (click)="onCloseConfirm()">Save</button>
</md-dialog-actions>

So here the I have mentioned ngModel in the input field by doing so it doesn't pre-populate the value{{ data.customerid }} if i remove that as I've written in second input field it does populate the value.If i mention ngModel why it doesn't give the pre-loaded value can anyone help me with this.

1

There are 1 answers

2
Ali Shahzad On

From your component, you can pass data to the Dialog like this:

dialogRef.componentInstance['data'] = { id: 123, name: 'Example' };