我尝试将 FormArray 传递到组件中。该组件模板迭代 FormArray 并显示数据。它允许用户添加到数组或删除项目。
子组件 ts
@Input()
public formArray!:FormArray;
Run Code Online (Sandbox Code Playgroud)
然后在模板中我尝试:
<div class="formRow child2" *ngFor="let year of formArray; let i=index">
List Number #{{i+1}}
<mat-form-field class="col3 no-bottom" appearance="fill">
<mat-label>{{ "YEAR" | translate }}</mat-label>
<input matInput placeholder="Year" mask="0000" [formControl]="year">
</mat-form-field>
<button mat-flat-button color="accent" (click)="addYear()">
<span class="material-icons">Add Year</button>
Run Code Online (Sandbox Code Playgroud)
但是,我收到一条错误消息:
Type 'FormArray' is not assignable to type 'NgIterable<any> | null | undefined'.
<div class="formRow child2" *ngFor="let year of formArray; let i=index">
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么会发生这种情况?高度赞赏。