Angular 如何使用动态formArray在html中创建表单

Angular 如何使用动态formArray在html中创建表单,angular,angular2-forms,Angular,Angular2 Forms,如何使用动态FormArray在HTML中生成表单,该表单是通过选择多个文件的数量生成的。每个文件都有自己的FormGroup。当我使用formControlName和formGroupName时,它显示的错误是 AttachComponent.html:45错误:formControlName必须与父formGroup指令一起使用。您将要添加一个formGroup 指令并向其传递现有的FormGroup实例(您可以在类中创建一个) 示例: 在你们班: this.myGroup=newformg

如何使用动态FormArray在HTML中生成表单,该表单是通过选择多个文件的数量生成的。每个文件都有自己的FormGroup。当我使用formControlName和formGroupName时,它显示的错误是

AttachComponent.html:45错误:formControlName必须与父formGroup指令一起使用。您将要添加一个formGroup 指令并向其传递现有的FormGroup实例(您可以在类中创建一个)

示例:
在你们班:
this.myGroup=newformgroup({
名字:newformcontrol()
});
在Function.webpackJsonp…/../forms/@angular/forms.es5.js.ReactiveErrors.controlParentException(forms.es5.js:4507)中
在FormControlName.webpackJsonp…/../forms/@angular/forms.es5.js.FormControlName.\u checkParentType(forms.es5.js:5396)
在FormControlName.webpackJsonp…/../forms/@angular/forms.es5.js.FormControlName.\u setUpControl(forms.es5.js:5403)
在FormControlName.webpackJsonp…/../../forms/@angular/forms.es5.js.FormControlName.ngOnChanges(forms.es5.js:5322)
在checkAndUpdateDirectiveInline(core.es5.js:10840)
在checkAndUpdateNodeLine(core.es5.js:12341)
在checkAndUpdateNode(core.es5.js:12284)
在debugCheckAndUpdateNode(core.es5.js:13141)
在debugCheckDirectivesFn(core.es5.js:13082)
在Object.eval[作为updateDirectives](AttachComponent.html:49)
我已经把我的代码以及截图

<button class="btn btn-primary pull-right" (click)="file.click()" type="button">Select File</button>
    <input name="file" #file type="file" multiple="multiple" (change)="onSelectDocument($event)" style="display: none;" />
选择文件
@Input()数据类型;
@输入()逻辑推理;
//此函数将在选择文件时调用
导出类上载AttachData{
选择:布尔型;
文档类型:字符串;
描述:字符串;
文件名:字符串;
boQueryReason:字符串;
boQueryComments:编号;
档案:档案;
构造函数(对象:任何){
this.select=obj&&obj.select?obj.select:false;
this.documentType=obj&&obj.documentType?obj.documentType:“”;
this.description=obj&&obj.description?obj.description:“”;
this.fileName=obj&&obj.name?obj.name:“”;
this.boQueryReason=obj&&obj.boQueryReason?obj.boQueryReason:“”;
this.boQueryComments=obj&&obj.boQueryComments?obj.boQueryComments:“”;
this.file=obj?obj:null;
}
}
onSelectDocument(事件:任何){
this.documentData=this.generateFormGroupArray(事件);
调试器
console.log(this.documentData);
}
generateFormGroupArray(事件):FormArray{
常量数据:FormArray=this.fb.array([]);
const fileList:fileList=event.target.files;
如果(fileList.length>0){
for(设i=0;i

附上文件
删除
上载文件
选择文件
挑选
文件类型
*
描述
*
文件名/注释
询问原因
BO查询评论
选择值
{{data.value}}
{{document.controls['fileName'].value}
选择值
{{data.value}}

我得到了一个解决方案,我不需要使用formArrayName、formGroupName、formControlName等

只需使用[formControl]即可,即

<tbody *ngIf="documentData">
        <tr *ngFor="let document of documentData.controls; let i=index;">
            <th>
                <span class="check">
                        <input type="checkbox" id="{{i}}" [formControl]="document.controls['selected']">
                        <label for="{{i}}"><span></span></label>
                </span>
            </th>
            <th></th>
            <th><input type="text" class="form-control" [formControl]="document.controls['description']"></th>
            <th>{{document.controls['fileName'].value}}</th>
            <th>5</th>
            <th><input type="text" class="form-control" [formControl]="document.controls['boQueryComments']"></th>
        </tr>
    </tbody>

{{document.controls['fileName'].value}
5.

我得到了一个解决方案,我不需要使用formArrayName、formGroupName、formControlName等

只需使用[formControl]即可,即

<tbody *ngIf="documentData">
        <tr *ngFor="let document of documentData.controls; let i=index;">
            <th>
                <span class="check">
                        <input type="checkbox" id="{{i}}" [formControl]="document.controls['selected']">
                        <label for="{{i}}"><span></span></label>
                </span>
            </th>
            <th></th>
            <th><input type="text" class="form-control" [formControl]="document.controls['description']"></th>
            <th>{{document.controls['fileName'].value}}</th>
            <th>5</th>
            <th><input type="text" class="form-control" [formControl]="document.controls['boQueryComments']"></th>
        </tr>
    </tbody>

{{document.controls['fileName'].value}
5.

我得到了不需要使用formArrayName、formGroupName、formControlName等的解决方案,etci得到了不需要使用formArrayName、formGroupName、formControlName等的解决方案