gndps
gndps

Reputation: 821

How to manually set values of nested form in Angular 2?

So I have a nested form like this:

form
    - address
        - city
        - state
        - pincode
    - name
    - phone

now I want to set the value of pincode programatically. How do I set it? I have checked it for the non nested form and found it here: But can't seem to find it for nested forms.

I have tried using dot notation for finding controls.

Upvotes: 0

Views: 254

Answers (2)

Akj
Akj

Reputation: 7231

Try something like this:

Refer Demo to understand form array

DEMO

Use index to get controls of FormArray.

form.controls.address.controls[i].controls.pincode.setValue('your_pin')

Upvotes: 2

Krishna Rathore
Krishna Rathore

Reputation: 9687

use form.get('controlName')

this.form.get('address').get('pincode').setValue(selected.id);

Upvotes: 2

Related Questions