Home > Blockchain >  MatDialog: How to output multiple values?
MatDialog: How to output multiple values?

Time:05-17

I have a dialog that contains 2 input fields. Once I submit the form, I want to show their values. So the values should be saved within the object myData.

This is how I want to show them:

<button mat-raised-button (click)="openDialog()">Pick one</button>
<div *ngIf="animal">
  <p>Favorite Animal: <i>{{myData.animal}}</i></p>
  <p>Favorite Fruit: <i>{{myData.fruit}}</i></p>
</div>

And this is how I tried to achieve my goal. It is entirely wrong but I hope it describes what I am trying to do.

<div mat-dialog-content>
  <p>What's your favorite animal?</p>
  <mat-form-field appearance="fill">
    <mat-label>Favorite Animal</mat-label>
    <input matInput [(ngModel)]="data">
  </mat-form-field>
  <p>What's your favorite fruit?</p>
  <mat-form-field appearance="fill">
    <mat-label>Favorite Fruit</mat-label>
    <input matInput [(ngModel)]="data">
</mat-form-field>
</div>
<div mat-dialog-actions>
  <button mat-button (click)="onNoClick()">No Thanks</button>
  <button mat-button [mat-dialog-close]="data" cdkFocusInitial>Ok</button>
</div>
import {Component, Inject} from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';

export interface DialogData {
  animal: string;
  fruit: string
}

/**
 * @title Dialog Overview
 */
@Component({
  selector: 'dialog-overview-example',
  templateUrl: 'dialog-overview-example.html',
})
export class DialogOverviewExample {
  myData: DialogData;

  constructor(public dialog: MatDialog) {}

  openDialog(): void {
    const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
      width: '250px'    
    });

    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
      this.myData.animal = result;
      this.myData.fruit = result; // I know that it makes no sense :)
    });
  }
}

@Component({
  selector: 'dialog-overview-example-dialog',
  templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog {
  constructor(
    public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
    @Inject(MAT_DIALOG_DATA) public data: DialogData,
  ) {}

  onNoClick(): void {
    this.dialogRef.close();
  }
}

See the code on StackBlitz

CodePudding user response:

You can do it by returning object from dialog.

html

              :
    <input matInput [(ngModel)]="data.animal">
              :
    <input matInput [(ngModel)]="data.fruit">
              :

ts

import {Component, Inject} from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog';

export interface DialogData {
  animal: string;
  fruit: string
}

/**
 * @title Dialog Overview
 */
@Component({
  selector: 'dialog-overview-example',
  templateUrl: 'dialog-overview-example.html',
})
export class DialogOverviewExample {
  myData: DialogData;

  constructor(public dialog: MatDialog) {}

  openDialog(): void {
    const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
      width: '250px'    
    });

    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
      if (result != null) {
        this.myData.animal = result.animal;
        this.myData.fruit = result.fruit;
      }
    });
  }
}

@Component({
  selector: 'dialog-overview-example-dialog',
  templateUrl: 'dialog-overview-example-dialog.html',
})
export class DialogOverviewExampleDialog {
  public data: DialogData = {animal: '', fruit: ''};

  constructor(
    public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
  ) {}

  onNoClick(): void {
    this.dialogRef.close();
  }
}

CodePudding user response:

Replace myData: DialogData; with:

myData: DialogData = {
  animal: '',
  fruit: ''
}

Both ngModels shouldn't have data only. They should be:

<input matInput [(ngModel)]="data.animal">
<input matInput [(ngModel)]="data.fruit">

Also, you should pass the correct values to the objects within your subscribe() method. It should looks like this:

dialogRef.afterClosed().subscribe(result => {
  console.log('The dialog was closed');
  this.myData.animal = result.animal;
  this.myData.fruit = result.fruit;
});

And the last thing that is missing in your code is the data object, which should be within your open() method. That should look like this:

const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
  width: '250px',
  data: {animal: this.myData.animal, fruit: this.myData.fruit}
});
  • Related