question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing Data with MatDialog to form

See original GitHub issue

hi I want passing multi data with mat dialog to form please help me

import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material';
import {DialogComponent } from '../../misc/dialog.component';

@Component({
  selector: 'app-chart',
  templateUrl: './chart.component.html',
  styleUrls: ['./chart.component.css']
})
export class ChartComponent implements OnInit {

  dialogResult = "";
  firstname: string = ;
  lastname: string;
  gender: boolean = true;

  constructor(public dialog: MatDialog) {  }

  ngOnInit() {
 }

  doChartSetting(){
    let dialogRef = this.dialog.open(ChartDialogComponent, {
        width:'600px',
        data: { firstname: this.firstname, lastname:this.lastname,this.gender}
    });    

    dialogRef.afterClosed().subscribe(result => {

      this.firstname= result;
      this.lastname= result;
      this.gender= result;
    });
    
  }
}

My Dialog:

mport { Component, OnInit, Inject , ViewChild , Input , Output, EventEmitter , AfterViewInit} from '@angular/core';
import { MatDialogRef,MAT_DIALOG_DATA } from '@angular/material';

@Component({
  selector: 'app-dialog',
  templateUrl: './dialog.component.html',
  styleUrls: ['./dialog.component.css']
})
export class DialogComponent implements OnInit {

  constructor(public dialogRef: MatDialogRef<DialogComponent>,@Inject(MAT_DIALOG_DATA) public data:any) { }

  ngOnInit() {
  }

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

}

dialog-component.html

<div class="dialog-wrapper">
	<h3 mat-dialog-title >
		Dialog
	</h3>
	<mat-dialog-content>
		mat-form-field>
			    <input matInput placeholder="firstname" [(ngModel)]="data.firstname">
			  </mat-form-field>

			  <mat-form-field>
			    <input matInput placeholder="lastname" [(ngModel)]="data.lastname">
			  </mat-form-field>

			  <mat-checkbox [(ngModel)]="data.gender">gender</mat-checkbox>


	</mat-dialog-content>
	<hr>
	<mat-dialog-actions>
		<button mat-button color="primary" [mat-dialog-close]="data" cdkFocusInitial>ذخیره</button>
		<button mat-button (click)="onClose()">close</button>
	</mat-dialog-actions>
</div>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
josephperrottcommented, Jan 18, 2018

Please keep GitHub issues for bug reports / feature requests. Better avenues for troubleshooting / questions are stack overflow, gitter, mailing list, etc.

2reactions
aloketewarycommented, Jan 18, 2018

You can always use like this:

componentData: any = { 
   firstname: 'Something', 
   lastname:'Important',
   gender: 'M',
   address : {
     city: 'somewhere'
   }
};

let dialogRef = this.dialog.open(ChartDialogComponent, {
        width:'600px',
        data: { comp: this.componentData }
    });  

and in your DialogComponent you can access it like:

 <mat-form-field>
        <input matInput placeholder="lastname" [(ngModel)]="data.comp.lastname">
 </mat-form-field>
<mat-form-field>
        <input matInput placeholder="cityAddress" [(ngModel)]="data.comp.address.city">
 </mat-form-field>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass data to a MatDialog | nerd.vision
You can pass any kind of data to the component by adding data to the MatDialog's open() options. this.dialog.open(PersonalDetailsComponent, {
Read more >
How to pass data to dialog of angular material 2 - Stack Overflow
1) add the import statement import { MatDialog} from '@angular/material'; · 2) add the property to the constructor params · 3) define the...
Read more >
Angular Material 9|8|7 Add Dialog Modal and Pass Data ...
In the class file, import the MatDialog class from @angular/material package to provide the open() method. This method takes the component we ...
Read more >
Angular Material Dialog: A Complete Example
Dialogs are often used to edit existing data. We can pass data to the dialog component by using the data property of the...
Read more >
Dialog | Angular Material
The MatDialog service can be used to open modal dialogs with Material ... If you want to share data with your dialog, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found