Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

javascript - angular 4 nested formArray Cannot find control with path: 'segmentRows3 -> 1 -> segmentId3'

I cannot push to an array. I have a segment(id, time) that can have one or several people (role, infos). The field are generated dynamically. On load the page shows the fields (see image below).
enter image description here

When try to add a segment I get error : ERROR TypeError: Cannot find control with path: 'segmentRows3 -> 1 -> segmentId3'
Here is code from the .ts file:

addSegment() {
let segmentRows3 = this.mySummaryForm.get('segmentRows3') as FormArray;
    segmentRows3.push(this.fb.array([
        this.fb.group({
            segmentTime3: '',
            segmentId3: '',             
            personRows3: this.fb.array([
            this.fb.group({
               personR3: '',
               personI3: ''
              })
            ])
        })
      ]));
  }   

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

The .html code

<div formArrayName="segmentRows3">
  <label><h2>New segment</h2></label>
  <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" 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>
        <button type="button" (click)="addPerson(i)" class="btn btn-info">Add a person</button><br><br> 
        <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="personI3">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><br><br>
                </div>                  
            </div>
        </div>
    </div>            
  </div>
</div>
<br><button type="button" (click)="addSegment()" class="btn btn-primary">Add a segment</button>   

When i try to add it adds the segment(id, time) but not the person (role, infos) unlike on load (image below), and throws error : ERROR TypeError: Cannot find control with path: 'segmentRows3 -> 1 -> segmentId3' . Why?

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're adding FormArray to segmentRow3 array whilst your template expects only FormGroup:

segmentRows3.push(this.fb.array([
                  ^^^^^^^^^^^^^^
                       why?
        this.fb.group({

So, try changing it to:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...