Pre*_*mar 2 angular angular-reactive-forms formarray
表单组
this.locationForm = this.formBuilder.group({
locationName: new FormControl(null, Validators.required),
locationURL: new FormControl(null, Validators.required),
workTiming: this.formBuilder.array([
this.formBuilder.group({
beginTime: new FormControl(null,Validators.required),
})
])
})
Run Code Online (Sandbox Code Playgroud)
HTML代码:
<div formArrayName="workTiming" >
<div *ngFor="let item of workTiming.controls;
let pointIndex=index" [formGroupName]="pointIndex">
<div class="container">
<mat-form-field class="responsive">
<input type="time" required formControlName="beginTime" matInput placeholder="Begin Time">
<mat-error *ngIf="workTiming.get('beginTime').hasError('required')"> Enter begin time </mat-error>
</mat-form-field>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我需要一些关于如何访问 mat-error 中“beginTime”的 formControl 名称的帮助,因为我使用的是 formArray,我不确定如何访问它。如果我在代码中给出这样的错误,我会收到如下错误
ERROR TypeError: Cannot read property 'hasError' of null
Run Code Online (Sandbox Code Playgroud)
感谢您的尝试,我找到了以下代码的解决方案:
<mat-error *ngIf="workTiming.controls[pointIndex].get('beginTime').hasError('required')"> Enter begin time </mat-error>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3000 次 |
| 最近记录: |