1

I am trying to created nested form group in Angular6.

serviceItemForm: FormGroup;
apiForm : FormGroup;

this.serviceItemForm = this.fb.group({
  'name': new FormControl('', Validators.required),
  'description': new FormControl('', Validators.required),
  'categoryIds': new FormControl([], Validators.required),
  'tags': [],
  'status': '',
  'orderFormType': new FormControl([], Validators.required),
  'orderFormKey': new FormControl([], Validators.required),
  'workflow': [],
  'orderFormAction': new FormControl('', Validators.required),
  'customUI': new FormControl(''),
  'api': this.fb.group({
    'apiurl': new FormControl(''),
    'apimethod': new FormControl(''),
    'headers': this.fb.array([]),
    'payload': '',
  }),
  'userGroup': []
});

this.apiForm = this.serviceItemForm.get('api');

Here this.apiForm is giving error like Type 'AbstractControl' is not assignable to type 'FormGroup'. Property 'controls' is missing in type 'AbstractControl'. In VSCode.

Please help how can I use nested form-group in angular-6

2 Answers 2

1

You can try below code:

serviceItemForm: FormGroup;
apiForm : FormGroup;

this.apiForm = this.fb.group({
    'apiurl': new FormControl(''),
    'apimethod': new FormControl(''),
    'headers': this.fb.array([]),
    'payload': '',
  });

this.serviceItemForm = this.fb.group({
  'name': new FormControl('', Validators.required),
  'description': new FormControl('', Validators.required),
  'categoryIds': new FormControl([], Validators.required),
  'tags': [],
  'status': '',
  'orderFormType': new FormControl([], Validators.required),
  'orderFormKey': new FormControl([], Validators.required),
  'workflow': [],
  'orderFormAction': new FormControl('', Validators.required),
  'customUI': new FormControl(''),
  'api': this.apiForm,
  'userGroup': []
});

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

2 Comments

How can I access this.apiForm in my html template
what problem you are getting in accessing this.apiForm in template?
0

If you check typeof of any nested (or parent) FormGroup it will return always object. so what you can do is just typecast your variable of type object like this -

apiForm : Object;
...
this.apiForm = this.serviceItemForm.get('api');

Else, You can typecast at the time of assigning value to a variable like this -

apiForm : FormGroup;
this.apiForm = (<FormGroup>this.serviceItemForm.get['api']);

2 Comments

But 'api' is this.fb.group. I tried your suggestion but still no success.
Yes, you are right. By doing apiForm : Object; error gone. But Even I am curious to know why this return of type object

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.