【发布时间】:2020-06-02 11:22:37
【问题描述】:
我尝试从我的组件中打开一个 Angular Material 对话框。但对话框在打开后立即关闭。
我知道 Stackoverflow 上有一些类似的问题,但他们的答案似乎对我不起作用。不确定从订阅的完整部分调用对话框功能是否是问题所在。
ExampleDialogComponent.ts
export class ExampleDialogComponent implements OnInit {
inputVal1: string;
inputVal2: string;
constructor(public dialogRef: MatDialogRef<ExampleDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any, private formBuilder: FormBuilder) {
this.inputVal1 = data.inputVal1;
this.inputVal2 = data.inputVal2;
}
ngOnInit() {
this.firstFormGroup = this.formBuilder.group({
firstCtrl: ['', Validators.required]
});
this.secondFormGroup = this.formBuilder.group({
secondCtrl: ['', Validators.required]
});
}
onCloseConfirm() {
setTimeout(() => {
this.dialogRef.close({
message: 'Confirm',
outputVal1: this.inputVal1,
outputVal2: this.inputVal2,
});
}, 0);
}
}
ExampleDialogComponent.html
<mat-step>
<ng-template matStepLabel>last step</ng-template>
<mat-dialog-actions>
<button mat-button [mat-dialog-close]="onCloseConfirm()">add</button>
<button mat-button mat-dialog-close>close</button>
</mat-dialog-actions>
</mat-step>
MainCompontent.ts
openModal() {
this.inputs = [];
this.http.getInputs().subscribe(
data => {
data.forEach(element => this.inputs.push(element));
},
(err) => {
console.log(err);
},
() => {
this.openAddDialog();
});
}
openAddDialog() {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.data = {
inputValue1: 'test 1',
inputValue2: 'test 2',
};
const dialogRef = this.dialog.open(ExampleDialogComponent, dialogConfig);
dialogRef.afterClosed().subscribe(result => {
console.log('Dialog was closed');
console.log('result: ' + result);
this.http.createResultinDB(result);
.subscribe(subData => console.log(subData), error => console.log(error));
});
}
getDeliveryPackageList(): Observable<any> {
return this.http.get(`${this.baseUrl}`);
}
非常感谢您的帮助。
【问题讨论】:
-
Stackblitz 将更有助于重现问题
-
您是否将其作为 entryComponent 添加到您的模块中?除非您使用的是 Angular 9,否则您需要这样做。看到这个...stackoverflow.com/questions/41519481/…
标签: angular typescript angular-material