Gem
Gem

Reputation: 545

Angular setValue is not a function in particular index

Angular I am facing this issue

ERROR TypeError: this.form.value.practiceSet[i].skillone.setValue is not a function

In this I need to change the value of the skillone inside the array but I am facing this issue how to fix the value my ts is

skillset(i){
this.form.value.practiceSet[i].skilltwo.setValue(idval)
}

My json format is


    {
    "game":"football",
     practiceSet:[
        {
           "skillone":"running",
           "skilltwo":"chasing"
         },{
            "skillone":"jump",
             "skilltwo":"gym"
         },
      ]
    }

I need to change the value in particular value of "skillone":"running" located in this.form.value.practiceSet[i].skilltwo.setValue(idval).. I got the value in "idval" but I got error

My HTML is


    <ng-container formArrayName="practiceSet" *ngFor="let group of practiceSetArray.controls; let i = index;">
        <tr [formGroupName]="i">
            <td>{{i+1}}</td>
            <td>
               <mat-form-field>
                  <input matInput readonly formControlName="Skill set"></mat-form-field>
             </td>

My onit (initializing)


    this.form = this.fb.group({
    
    practiceSet: this.fb.array([
            this.practiceSetArray()
          ])
        })
    
    
    
    practiceSetArray(){
        let group = new FormGroup({
          skillone: new FormControl(''),
          skilltwo: new FormControl('')
    
    })
    }

Upvotes: 1

Views: 6775

Answers (2)

Msk Satheesh
Msk Satheesh

Reputation: 1536

try using:

get your formarray and assign:

(<FormArray>this.form.get("practiceSet")).controls[i]
        .get("skilltwo")
        .setValue(idval);

Here the stackblitz example:

https://stackblitz.com/edit/form-array-angular-2wmurr?file=src/app/app.component.ts

Upvotes: 1

Owen Kelvin
Owen Kelvin

Reputation: 15083

You are trying to call setValue() on an Object.

I believe you are using a reactive forms approach. Consider the line this.form.value, after calling .value you have a an Object. To get a FormGroup instead do below

  this.form.get('practiceSet').controls[i].get('skilltwo').setValue(idval)

Upvotes: 1

Related Questions