반응형
각도 물질 대화상자에서 부모 구성요소로 데이터를 전달하는 방법?
각도 6을 사용하고 있는데 대화창이 열리는 버튼이 있습니다.제 대화 상자에는 사용자의 데이터를 가져오는 양식이 있고 그 다음에 제출하고 취소하는 두 개의 버튼이 있습니다.양식의 데이터를 콘솔에 표시하려고 했는데 정의되지 않은 상태로 반환됩니다. 무슨 문제가 있습니까?다음은 코드의 일부입니다.
main.component.ts:
import { Work } from '../../../../classes/work_shift';
import { DialogContentComponent} from './dialog-content/dialog-content.component';
export class WorkShiftsComponent implements OnInit {
shifts: Work[];
name: string;
start: string;
end: string;
constructor(public dialog: MatDialog, private shiftService: WorkShiftsService) { }
ngOnInit() {
}
openDialog() {
const dialogRef = this.dialog.open(DialogContentComponent, {
width: '640px',
disableClose: true,
data: {name: this.name, start: this.start, end: this.end}
});
dialogRef.afterClosed().subscribe(result => {
console.log('The dialog was closed');
console.log(result);//returns undefined
});
}
}
dialogContent.component.html:
<mat-dialog-content>
<form class="example-form">
<div fxLayout="column" fxLayoutAlign="space-around" class="form">
<div class="input">
<mat-form-field class="input4">
<input matInput placeholder="Shift name">
</mat-form-field>
</div>
<div>
<mat-form-field class="input input2">
<input matInput placeholder="Start" atp-time-picker>
</mat-form-field>
<mat-form-field class="input input2">
<input matInput placeholder="End" atp-time-picker >
</mat-form-field>
</div>
<br/>
</div>
</form>
</mat-dialog-content>
<mat-dialog-actions>
<button class="mat-button" mat-button (click)="onClose()">Cancel</button>
<button class="mat-button" mat-button [mat-dialog-close]="data" cdkFocusInitial color="primary">Create</button>
</mat-dialog-actions>
전체 자습서 링크 확인
근접() 방법으로 Dialog 구성 요소에서 상위 항목으로 데이터 전달
//dialog-box.component.ts
import { Component, Inject, Optional } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
export interface UsersData {
name: string;
id: number;
}
@Component({
selector: 'app-dialog-box',
templateUrl: './dialog-box.component.html',
styleUrls: ['./dialog-box.component.css']
})
export class DialogBoxComponent {
action:string;
local_data:any;
constructor(
public dialogRef: MatDialogRef<DialogBoxComponent>,
//@Optional() is used to prevent error if no data is passed
@Optional() @Inject(MAT_DIALOG_DATA) public data: UsersData) {
console.log(data);
this.local_data = {...data};
this.action = this.local_data.action;
}
doAction(){
this.dialogRef.close({event:this.action,data:this.local_data});
}
closeDialog(){
this.dialogRef.close({event:'Cancel'});
}
}
그런 다음 상위 구성 요소의 이벤트 및 데이터 개체/값을 다시 가져옵니다.
//app.component.ts
import { Component, ViewChild } from '@angular/core';
import { MatDialog, MatTable } from '@angular/material';
import { DialogBoxComponent } from './dialog-box/dialog-box.component';
export interface UsersData {
name: string;
id: number;
}
const ELEMENT_DATA: UsersData[] = [
{id: 1560608769632, name: 'Artificial Intelligence'},
{id: 1560608796014, name: 'Machine Learning'},
{id: 1560608787815, name: 'Robotic Process Automation'},
{id: 1560608805101, name: 'Blockchain'}
];
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
displayedColumns: string[] = ['id', 'name', 'action'];
dataSource = ELEMENT_DATA;
@ViewChild(MatTable,{static:true}) table: MatTable<any>;
constructor(public dialog: MatDialog) {}
openDialog(action,obj) {
obj.action = action;
const dialogRef = this.dialog.open(DialogBoxComponent, {
width: '250px',
data:obj
});
dialogRef.afterClosed().subscribe(result => {
if(result.event == 'Add'){
this.addRowData(result.data);
}else if(result.event == 'Update'){
this.updateRowData(result.data);
}else if(result.event == 'Delete'){
this.deleteRowData(result.data);
}
});
}
addRowData(row_obj){
var d = new Date();
this.dataSource.push({
id:d.getTime(),
name:row_obj.name
});
this.table.renderRows();
}
updateRowData(row_obj){
this.dataSource = this.dataSource.filter((value,key)=>{
if(value.id == row_obj.id){
value.name = row_obj.name;
}
return true;
});
}
deleteRowData(row_obj){
this.dataSource = this.dataSource.filter((value,key)=>{
return value.id != row_obj.id;
});
}
}
대화 상자로 데이터 보내기 및/또는 받기
대화상자 구성요소
// HTML
<div [innerHTML]="data"></div>
<button (click)="cancel()">No</button>
<button (click)="confirm()">Yes</button>
// Typescript
export class DialogComponent {
// receive data from parent using 'MAT_DIALOG_DATA'
constructor(@Inject(MAT_DIALOG_DATA) public data: string,
private dialogRef: MatDialogRef<DialogComponent>) { }
cancel() {
// closing itself and sending data to parent component
this.dialogRef.close({ data: 'you cancelled' })
}
confirm() {
// closing itself and sending data to parent component
this.dialogRef.close({ data: 'you confirmed' })
}
}
상위 구성요소
constructor(private dialog: MatDialog) { }
// method to open dialog
openDialog() {
let dialogRef = this.dialog.open(DialogComponent, {
data: `Are you sure you want to delete?`
})
dialogRef.afterClosed().subscribe(res => {
// received data from dialog-component
console.log(res.data)
})
}
공통 팝 서비스:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { MatDialogRef, MatDialog, MatDialogConfig } from '@angular/material';
import { PupupFormComponent } from './pupup-form/pupup-form.component'
@Injectable()
export class CommonModelService {
animal: string;
name: string;
date1: any;
date2: any
constructor(public dialog: MatDialog) { }
openDialog(): Observable<any> {
const dialogRef = this.dialog.open(PupupFormComponent, {
width: '250px',
data: { name: this.name, animal: this.animal, date1: this.date1, date2: this.date2 }
});
return dialogRef.afterClosed();
}
}
parent.component.ts:
import { Component, Inject } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { CommonModelService } from './common-model.service'
export interface DialogData {
animal: string;
name: string;
}
@Component({
selector: 'dialog-overview-example',
templateUrl: 'dialog-overview-example.html',
styleUrls: ['dialog-overview-example.css'],
})
export class DialogOverviewExample {
animal: string;
name: string;
constructor(private commModel: CommonModelService) { }
openDialog() {
this.commModel.openDialog().subscribe(data => {
console.log(data);
});
}
}
parent.component.html:
<button mat-raised-button (click)="openDialog()">Open Form</button>
팝업 양식. html:
<div mat-dialog-content>
<p>What's your favorite animal?</p>
<mat-form-field>
<input matInput [(ngModel)]="data.animal">
</mat-form-field>
<mat-form-field>
<input matInput type="time" atp-time-picker [(ngModel)]="data.date1">
</mat-form-field>
<mat-form-field>
<input matInput type="time" atp-time-picker [(ngModel)]="data.date2">
</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>
대화상자의 이벤트 저장을 구독하는 것보다 EventEmit를 사용합니다.일이 좀 더 많지만 내 문제를 해결하는데 더 적합합니다.대부분 Dlg에서 수정한 개체를 전달하고 수정한 개체를 전달하고 싶지 않을 수도 있기 때문입니다.이 솔루션에는 일종의 Deep Copy 솔루션이 필요했습니다.
대화상자에서:
import { EventEmitter } from '@angular/core';
@Component({...})
export class DlgComponent {
data: any
data: deepCopy(data) // some kindof deep copy solution like lodash
onSave = new EventEmitter();
....
save() {
this.onSave.emit(this.data)
}
상위 구성 요소에서:
import { MatDialog, MatDialogConfig, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
@Component({...})
export class ParentComponent {
data: any
constructor() {
public dialogRef: MatDialogRef<DlgComponent>,
}
openDlg() {
let dialogConfig = new MatDialogConfig();
const dialogRef = this.dialog.open(DlgmComponent, dialogConfig);
dialogRef.componentInstance.onSave.subscribe(data=> {
this.data = data
})
}
}
대화상자에서:
close(): void {
this.dialogRef.close(data); //on close pass data to parent
}
상위 구성 요소에서:
openDialog(row){
const dialogRef = this.dialog.open(DialogComponent, {
width: '880px',
height: '480px',
disableClose: true,
data: data
});
dialogRef.afterClosed().subscribe(res => {
console.log("value from dialog",res) //data from dialog
this.headerDialogValue = res;
});
}
아래 솔루션이 저에게 적합합니다.
언급URL : https://stackoverflow.com/questions/51815455/how-to-pass-data-from-angular-material-dialog-to-parent-component
반응형
'programing' 카테고리의 다른 글
| 이동식 좌/우 스와이프를 이용하여 부트스트랩 회전목마 슬라이더를 만드는 방법 (0) | 2023.09.26 |
|---|---|
| C#에서 참조 커서를 반환하는 Oracle Function 실행 (0) | 2023.09.26 |
| MariaDB: 권한이 없는 'root' 및 ' 둘 다(접근 거부 1044 및 1227) (0) | 2023.09.26 |
| 함수 소요시간 측정 : clock_gettime (0) | 2023.09.26 |
| 병합 커밋에 대한 차이점을 "깃"으로 보여주는 방법은? (0) | 2023.09.21 |

