angularformarrayangular-abstract-control

Selector to determine if FormArray's controls property is a [FormControl] or [FormGroup]?


I have inherited an angular code file that I am debugging. When I log the FormArray that I need to evaluate, it logs out as one of two types of FormArray controls: [FormControl] or [FormGroup].

Here is the part of the function that I'm logging:

  someFunction(data: any, useForm: any = false): any {
   const form = useForm || <FormGroup>this.formGroup;

   forEach(data, (value, key) => {
    // Check nested FormArray items that include FormControls (Would likely be from mat-table).
   const formArray = (<FormGroup>form).get(String(key));
   console.log('formArray > ', formArray);

   if (isArray(value) && formArray.hasOwnProperty('controls') && [HELP]) 

In one use case, when a mat-table is being loaded that contains form controls, I get the following in the log:

enter image description here

In the another use case, such as a checkboxes control with multiple options, I get this:

enter image description here

I need to figure out how to filter this last line of code, the IF statement's [HELP] condition, so that it only matches the condition where the FormArray's controls property contains a [FormGroup] and not a [FormControl]


Solution

  • The instanceof operator tests the types:

    if (... && (<FormArray>formArray).controls[0] instanceof FormGroup) { ... }