【发布时间】:2020-04-23 02:39:17
【问题描述】:
我正在尝试从对话框提交表单,但 form.value 是 null。我不知道缺少什么。我提供了一些代码和DEMO 作为您的参考。
HTML
<div>
<button mat-raised-button (click)="openDialog()">Pick one</button>
</div>
<ng-template #callAPIDialog>
<form #userForm="ngForm" (ngSubmit)="onSend(form.value)">
<h2 matDialogTitle>Add Organization</h2>
<mat-form-field>
<input matInput [(ngModel)]="organisationName" placeholder="Your organization" [ngModelOptions]="{standalone: true}">
<input matInput [(ngModel)]="enabled" [(value)]="Y" [ngModelOptions]="{standalone: true}">
</mat-form-field>
<mat-dialog-actions align="end">
<button mat-button matDialogClose="no">Cancel</button>
<button type="submit" mat-button matDialogClose="yes">Submit</button>
</mat-dialog-actions>
</form>
</ng-template>
组件
openDialog() {
let dialogRef = this.dialog.open(this.callAPIDialog);
dialogRef.afterClosed().subscribe(result => {
if (result !== undefined) {
if (result !== 'no') {
const enabled = "Y"
console.log(result);
} else if (result === 'no') {
console.log('User clicked no.');
}
}
})
}
onSend(form: NgForm){
let data = form.value;
console.log(data);
}
【问题讨论】:
-
我可以看到你在控制台有很多错误首先尝试解决它们
-
好的,谢谢。我会解决的
-
好的,我正在努力解决它
-
请看看我的方法可能对你有帮助
标签: angular typescript angular-material