【问题标题】:Angular 4 Nested FormArrays Cannot read property 'push' of nullAngular 4嵌套FormArrays无法读取null的属性'push'
【发布时间】:2018-10-11 16:36:36
【问题描述】:

无法推送到数组。 我有一个可以有一个或几个人(角色,信息)的段(id,时间)。该字段是动态生成的。加载时页面显示字段(见下图)。

尝试添加人员时出现错误:ERROR TypeError: Cannot read property 'push' of null
这是 .ts 代码:

addPerson(index: number) {
//let personRows3 = <FormArray>this.mySummaryForm.get('personRows3'); 
let personRows3 = <FormArray>this.mySummaryForm.get(`segmentRows3.${index}.personRows3`)
personRows3.push(this.fb.group({
    personR3: '',
    personI3: ''
}));

}

  segmentRows3: this.fb.array([
    this.fb.group({
        segmentId3: '',
        segmentTime3: '',
        personRows3: this.fb.array([
        this.fb.group({
           personR3: '',
           personI3: ''
          })
        ])
    })
  ]),

.html 代码

<div formArrayName="segmentRows3">
  <div *ngFor=" let segmentRow of mySummaryForm.controls.segmentRows3.controls; let i=index " > 
    <div  class="form-group" [formGroupName]="i" >   {{i+1}}
    <label for="segmentId3">Segment ID
        <select formControlName="segmentId3"  formControlName="segmentId3" placeholder="pick" type="text" id="segmentId3" class="form-control" [(ngModel)]="levelNumSegment3" (ngModelChange)="toNumberSegment3()">
            <option *ngFor="let level of segmentId" [value]="level.num">{{level.name}}</option>
        </select> 
    </label>      
    <label for="segmentTime3">Segment time
        <input formControlName="segmentTime3" type="text" id="segmentTime3" class="form-control" placeholder="select a time" (ngModelChange)="onChange($event)">
    </label>    
        <div formArrayName="personRows3">
            <div *ngFor=" let personRow of segmentRow.controls.personRows3.controls; let j=index " >
                <div  class="form-group" [formGroupName]="j" >   {{j+1}}    
                    <label for="personR3">person Role 
                    <input formControlName="personR3" [typeahead]="personRole" [typeaheadOptionsLimit]="10" [typeaheadMinLength]="0" type="text" id="personR3" class="form-control" placeholder="select a role" (ngModelChange)="onChange($event)" >
                    </label>
                    <label for="personI">Person infos
                    <input formControlName="personI3" [typeahead]="states" [typeaheadOptionsLimit]="10" [typeaheadMinLength]="0" type="text" id="personI3" class="form-control" placeholder="select infos" (ngModelChange)="onChange($event)" >
                    </label>
                    <label><span (click)="deletePerson(j)" class="btn btn-danger">Remove</span></label>
                </div> 
            </div>
        </div>
        <br><button type="button" (click)="addPerson(j)" class="btn btn-info">Add a person</button><br><br><br>
    </div> 
  </div>
</div>

【问题讨论】:

    标签: javascript angular nested push formarray


    【解决方案1】:

    当您的 personRows3 为 null 或没有元素时,会出现上述错误。

    在推送元素之前添加一个检查,

    if(personRows3){
      personRows3.push(this.fb.group({
        personR3: '',
        personI3: ''
      }));
    }
    

    【讨论】:

    • 谢谢。当我尝试添加一个人时,它似乎没有看到索引(未定义)。你能帮忙吗?
    • 您应该在 ngFor 中有按钮来获取要传递的索引 j。
    • 如果我把它放在 ngfor 中,它会与每个添加的人一起显示。
    • 否则 j 将始终未定义
    • 好的,谢谢。将继续努力。我还有一个问题:当我尝试添加一个片段时,我遇到了这个错误:ERROR Error: Cannot find control with path: 'segmentRows3 -&gt; 1 -&gt; segmentId3' 为什么?
    猜你喜欢
    • 2018-03-27
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 2021-11-14
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    相关资源
    最近更新 更多