我在用数据更新表单时遇到了一些麻烦。我有一个嵌套formGroup
在另一个中formGroup
,我得到的数据没有更新表单,它仍然是空的。我可以在日志中看到数据,所以表单更新有问题。
这是我的组件:
this.editForm = this.formBuilder.group({
id: [],
date: ['', Validators.required],
checkboxValue: this.formBuilder.group({
closed: [],
openFrom: [''],
openTo: [''],
})
});
console.log(this.editForm.value);
this.httpClientService.getIrregularDaysById(+irregDayId)
.subscribe(data => {
this.irregDay = data;
this.irregDay.openFrom = this.formatTime(this.irregDay.openFrom);
this.irregDay.openTo = this.formatTime(this.irregDay.openTo);
// this.irregDay.date = this.formatDate(this.irregDay.date);
this.irregDay.closed = this.formatClosed(this.irregDay.closed);
this.editForm.patchValue(data);
console.log('getIrregularDaysById: ', data);
console.log(data.date);
});
和我的 HTML:
<div class="col-md-6">
<h2 class="text-center">Edit irregular day</h2>
<form [formGroup]="editForm" (ngSubmit)="onSubmit()">
<div class="material-input">
<mat-form-field class="form-group" appearance="outline">
<input matInput readonly [matDatepicker]="picker" placeholder="Please pick a date" formControlName="date">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker touchUi #picker disabled="false"></mat-datepicker>
<mat-error *ngIf="editForm.get('date').errors && (editForm.get('date').touched)">
Date is required
</mat-error>
</mat-form-field>
</div>
<div formGroupName="checkboxValue">
<div class="form-group">
<mat-checkbox formControlName="closed" class="form-control" color="primary" [(ngModel)]="disabled">
Closed</mat-checkbox>
</div>
<div class="material-input">
<mat-form-field class="form-group" appearance="outline">
<mat-label>Open from</mat-label>
<div class="timepicker">
<input matInput readonly [disableClick]="true" placeholder="Open from" formControlName="openFrom"
[ngxTimepicker]="startTimePicker" [format]="24" [disabled]="disabled">
<ngx-material-timepicker #startTimePicker></ngx-material-timepicker>
<ngx-material-timepicker-toggle [for]="startTimePicker"></ngx-material-timepicker-toggle>
</div>
<mat-error *ngIf="editForm.get('openFrom')?.errors && (editForm.get('openFrom').touched)">
This field is required
</mat-error>
</mat-form-field>
</div>
<div class="material-input">
<mat-form-field class="form-group" appearance="outline">
<mat-label>Open to</mat-label>
<div class="timepicker">
<input matInput readonly [disableClick]="true" placeholder="Open to" formControlName="openTo"
[ngxTimepicker]="startTimePicker2" [format]="24" [disabled]="disabled">
<ngx-material-timepicker #startTimePicker2></ngx-material-timepicker>
<ngx-material-timepicker-toggle [for]="startTimePicker2"></ngx-material-timepicker-toggle>
</div>
<mat-error *ngIf="editForm.get('openTo')?.errors && (editForm.get('openTo').touched)">
This field is required
</mat-error>
</mat-form-field>
</div>
</div>
<button mat-raised-button color="primary" type="button" class="btn btn-success" [disabled]="!editForm.valid"
(click)="onSubmit()" routerLink="/irregulardays">Update</button>
</form>
</div>