我试图在我的组件中迭代一个formArray,但我得到了以下错误

Error: Cannot find control with unspecified name attribute

下面是我的类文件中的逻辑

export class AreasFormComponent implements OnInit {
    public initialState: any;
    public areasForm: FormGroup;

    constructor(private fb: FormBuilder) { }

    private area(): any {
      return this.fb.group({
          name: ['', [Validators.required]],
          latLong: ['', [Validators.required]],
          details: ['', [Validators.required]]
      });
    }

    public ngOnInit(): void {
        this.areasForm = this.fb.group({
            name: ['', [Validators.required]],
            areas: this.fb.array([this.area()])
        });
    }
}

还有我的模板文件

<form class="areas-form" [formGroup]="areasForm" (ngSubmit)="onSubmit(areasForm.values)">
    <md-input-container class="full-width">
        <input mdInput placeholder="Location Name" type="text" formControlName="name" required>
        <md-error *ngIf="areasForm.get('name').hasError('required')">Please enter the locationName</md-error>
    </md-input-container>
    <md-grid-list cols="1" [formArrayName]="areas">
        <md-grid-tile formGroupName="i"  colspan="1" rowHeight="62px" *ngFor="let area of areasForm.controls.areas.controls; let i = index ">
            <md-grid-list cols="3" rowHeight="60px">
                <md-grid-tile colspan="1">
                    <md-input-container class="full-width">
                        <input mdInput placeholder="Area Name" type="text" formControlName="name" required>
                        <md-error *ngIf="areasForm.get('areas').controls[i].name.hasError('required')">Please enter the area name</md-error>
                    </md-input-container>
                </md-grid-tile>
                <md-grid-tile colspan="1">
                    <md-input-container class="full-width">
                        <input mdInput placeholder="details" type="text" formControlName="details" required>
                        <md-error *ngIf="areasForm.get('areas').controls[i].name.hasError('required')">Please enter the locationName</md-error>
                    </md-input-container>
                </md-grid-tile>
                <md-grid-tile colspan="1">
                    <button md-fab (click)="remove(i)"><md-icon>subtract</md-icon>Remove Area</button>
                </md-grid-tile>
            </md-grid-list>
        </md-grid-tile>
    </md-grid-list>
    <button type="submit" [disabled]="areasForm.invalid" md-raised-button color="primary">Submit</button>
</form>

推荐答案

从以下位置卸下括号

[formArrayName]="areas" 

并且仅限使用

formArrayName="areas"

这是因为有了[ ],你试图绑定一个variable,这不是.请注意您的提交,它应该是:

(ngSubmit)="onSubmit(areasForm.value)"

而不是areasForm.values.

Angular相关问答推荐

如何使用新的@for遍历Angular 为17的对象?

Angular KendoGrid RowReorderable,drop不适用于新添加的行

如何重新显示ngbAlert(Bootstrap widgest for Angular)消息后再次驳回它

Angular -移位在2个示波器中读取

Angular14:支持旧版浏览器的官方方式是什么?

Angular:如何在根组件中使用 ngx-translate?

有条件地取消所有内部可观察对象或切换到仅发出一个值的新对象

Observable .do() 运算符 (rxjs) 的用例

如何检测滚动到html元素的底部

res.json() 不是函数

如何在 Angular 5 HttpInterceptor 中添加多个标头

如何在Angular 4中为数字管道指定语言环境千位分隔符

NgrxStore 和 Angular - 大量使用异步管道或在构造函数中只订阅一次

请求 http.GET 时发送的 Angular2 OPTIONS 方法

Angular 2中基于Condition条件的点击事件

如何在 Angular 6 中使用 mouseover 和 mouseout

Angular:找不到不同的支持对象[object Object]

ng-template 上的 *ngFor 不输出任何内容

如何在 Angular Material 中设置图标的 colored颜色 ?

Angular2:子组件访问父类变量/函数