patch Value in a nested form control using angular2

K Oul picture K Oul · Jun 2, 2017 · Viewed 11.2k times · Source

I need to set a value in a nested control in a FormBuiler and the model is the following:

this.addAccForm = this.fb.group({
      accid: ['', Validators.required],
      status: '',
      cyc: this.fb.array([
        this.initCyc(),
      ])
    })

initCyc() {
      return this.fb.group({
        cycid: ['', Validators.required],
        name: ['', Validators.required],
        description: ['', Validators.required],
        status: ['', Validators.required],
        det: this.fb.group({
            dcycid: ['', Validators.required],
            status: ['', Validators.required]
        })
      })

I need to set a value cycid and also dcycid but I m stuck to it, I've tried to use the following line , but it does NOT help :

this.addAccForm.patchValue({cyc: { [0]: {cycid: 1234567 }}});

//

this.addAccForm.patchValue({cyc: { [0]: { det : {dcycid: 9876543}}}});

Any idea how it should be?

Answer

Alexandre picture Alexandre · Nov 27, 2017

This did the trick for me:

this.addAccForm.patchValue({'cyc': {cycid: 1234567 }});