【问题标题】:How can I get 'valueChanges' event for 'name' formcontrol from addUserInfoFormGroup如何从 addUserInfoFormGroup 获取“name”表单控件的“valueChanges”事件
【发布时间】:2020-02-06 10:24:41
【问题描述】:

为了在任何表单控件上使用 mat-autocomplete,我们需要将 valueChanges 事件获取到该特定表单控件。我的 formcontrol 存在于 formGroup 中,它再次使用 formArray,如下所示。我需要从 addUserInfoFormGroup 方法获取的 'name' formcontrol 的 valueChanges 事件。

我正在使用 (this.myForm.get('usersInfo') as FormArray).controls[0].controls.name.valueChanges 来获取 valueChanges 事件,但它不起作用。

我的 ts 文件

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 );  
}  

【问题讨论】:

  • 你检查我的答案了吗? ??????????
  • 是的@malbarmavi 我已经检查过了,它现在正在工作

标签: angular angular-material mat-autocomplete


【解决方案1】:

这样试试

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;  
}  

【讨论】:

  • 感谢@malbarmavi,该事件现在触发了。
猜你喜欢
  • 1970-01-01
  • 2017-12-07
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多