填写表单中的对象数组

时间:2018-04-10 23:14:48

标签: angular forms typescript angular-ngmodel angular-reactive-forms

我有一个在点击时被调用的函数,我有一个对象数组,我试图传递给我的表单。

[{baseline: true, order: 0, query: "string", sourceId: "string", sourcePath: "string", sourceType:'value1'}, {baseline: false, order: 1, query: "string", sourceId: "string", sourcePath: "string", sourceType:'value2'},{baseline: false, order: 1, query: "string", sourceId: "string", sourcePath: "string", sourceType:'value2'}]

我的功能如下:

pipelines = [];
constructor( private _fb: FormBuilder) {
this.newForm = _fb.group({
   query:'',
   sourceId:'',
   sourcePath:'',
   sourceType: ''
   });
}


getFormgroup(name): boolean {
let val = name;
let index = this.getdata.findIndex(obj => obj.configName === val);
console.log(this.getdata[index]['sources']);
this.pipelines = this.getdata[index]['sources'];
console.log(this.pipelines);
return true;

}

   a (click)="getFormgroup(mem.configName) ? addtionalInfo = true  addtionalInfo = false" class="clickable">{{mem.configName}}</a>
    <form [formGroup] ="newForm">
             <div *ngFor="let pipes of pipelines; let i=index;">
    <select name = "sourceType" formControlName="sourceType [(ngModel)]="pipes[i].sourceType">
       <option disabled selected value="">Select Source Type</option>
       <option value="value1">value1</option>
       <option value="value2">value2</option>
       <option value="value3">value3</option>
   </select>
     <input name = "sourceId" formControlName="sourceId" [(ngModel)]="pipes[i].sourceId" type="text">

    <input name="sourcePath" formControlName="sourcePath" [(ngModel)]="pipes[i].sourcePath">

    <textarea name="query" formControlName="query" [(ngModel)]="pipes[i].query">{{query}}</textarea>

    </div>
    </form>

所以基本上我在pipelines中存储对象数组,当调用该函数时,我尝试遍历管道并在下拉列表和文本框中显示值。 但是,我收到Cannot read property 'sourceType' of undefined错误,并且这些框为空,没有填充任何值。

0 个答案:

没有答案