1

In order to use mat-autocomplete on any form control we need to fetch the valueChanges event to that particular form control. My formcontrol is present inside a formGroup which again uses formArray as shown below. I need valueChanges event for 'name' formcontrol which is fetched from addUserInfoFormGroup method.

I am using (this.myForm.get('usersInfo') as FormArray).controls[0].controls.name.valueChanges to fetch the valueChanges event, but it doesn't work.

my ts file

ngOnInit() {      
           this.myForm= new FormGroup({   
             usersInfo: this.fb.array([this.addUserInfoFormGroup()])   
           });    
}   

addUserInfoFormGroup() : FormGroup{   
      let userInfo = {   
         'name': ['',Validators.required],  
         'age': ['',Validators.required],  
         'role': ['',Validators.required],  
       };  
      return this.fb.group(userInfo );  
}  

Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91

1 Answers1

0

Try like this

addUserInfoFormGroup() : FormGroup{   
    cosnt userInfo = {   
         'name': ['',Validators.required],  
         'age': ['',Validators.required],  
         'role': ['',Validators.required],  
    };  
    const fg = this.fb.group(userInfo );
    fg.get('name').valueChanges.subscribe(...);

    return fg;  
}  
Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91