【发布时间】:2019-08-31 11:51:09
【问题描述】:
我是角度的初学者。问题实际上出在 EmployeeComponent.ts 中,如果我在 ngOnInit() 中调用 this.resetForm(); 则对话框的控件会在页面加载期间显示......但在编辑期间,当它来自雇员列表时,service.formData 始终设置回 null 我不希望这种情况发生。我希望实现这两种功能。
如果我在ngOnInit() 中remove this.resetForm();,则会出现以下错误。 (但编辑工作正常)注意:service.formData 包含字段 id、description 和 active
控制台日志错误:EmployeeComponent_Host.ngfactory.js? [sm]:1 错误 TypeError:无法读取未定义的属性“id” 在 EmployeeComponent.push。
EmployeeComponent.ts
ngOnInit() {
this.resetForm();
}
resetForm(form ? : NgForm) {
if (form != null)
form.resetForm();
this.service.formData = {
id: null,
description: '',
active: true,
}
console.log(this.service.formData.id);
}
EmployeeListComponent.ts
onEdit(emp: Employee) {
this.service.formData = Object.assign({}, emp);
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.width = "50%";
this.dialog.open(EmployeeComponent, dialogConfig);
}
Employee.component.html
<form #form="ngForm" autocomplete="off" >
<mat-grid-list cols="2">
<input type="hidden" name="id" #id="ngModel" [(ngModel)]="service.formData.id">
<mat-form-field>
<input name="description" matInput #description="ngModel" [(ngModel)]="service.formData.description" placeholder="Employee" required>
</mat-form-field>
<mat-checkbox matInput #active="ngModel" [(ngModel)]="service.formData.active" name="active">Active</mat-checkbox>
</mat-grid-list>
<button mat-raised-button color="primary" type="submit" [disabled]="form.invalid" (click)="onSubmit(form)">Submit</button>
<button mat-raised-button color="warn" style="margin-left: 6px;" (click)="onClear(form)">Clear</button>
</form>
【问题讨论】:
标签: angular google-cloud-firestore angular7