0

I am working on angular 4 application and I am using reactive forms. I am getting one issue while creating new form control.

Typescript:

couponForm : FormGroup;
"industries": [
            {
                "id": 1,
                "name": "Home Cleaning",
                "is_active": true,
                "forms": [
                {
                    "id": 1,
                    "name": "Form 1",
                }
            },
            {
                "id": 2,
                "name": "Office Cleaning",
                "is_active": true,
                "forms": [
                {
                    "id": 2,
                    "name": "Form 2",
                 }
            }
];

constructor(private formBuilder: FormBuilder)
{
   this.couponForm = this.formBuilder.group({});
}

addIndustriesControl()
{
   this.couponForm.addControl('applicable_with_industries', this.formBuilder.array([]));

   let industriesArray =
 <FormArray>this.couponForm.controls['applicable_with_industries'];

   this.globalSettings.industries.forEach((industry, index) =>{
      let control = this.formBuilder.group({
         industry_id   : [industry.id],
         status        : [],
         forms         : this.formBuilder.array([])
      });

      industriesArray.push(control);
  });      
}

I want to add new controls in forms array inside each industry. Can any one please tell me how I can do it?

5
  • i think you are looking for something like this . if you want deep nesting i have a stackblitz link in that page too Commented Sep 11, 2017 at 5:43
  • Sorry, But I didn't get any thing from that link. Commented Sep 11, 2017 at 6:11
  • didnt get anything means ? to make it short check stackblitz.com/edit/deep-nested-reactive-form Commented Sep 11, 2017 at 6:13
  • When I am using this code, let formArray = <FormArray>this.couponForm.get('applicable_with_industries').controls[index].get('forms');. Getting this error Property 'controls' does not exist on type 'AbstractControl'. Commented Sep 11, 2017 at 6:50
  • Thank you so much for your support. With your help I have solved the issue easily. Commented Sep 11, 2017 at 10:04

1 Answer 1

1

let array = (this.couponForm.controls['applicable_with_industries']).controls[index].get('forms');

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.