最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the name of a form control in reactive forms? - Stack Overflow

programmeradmin2浏览0评论

So i currently have a reactive form with controls nested inside of it. Something like this:

costForm = new FormGroup({
  year1: new FormGroup({
    mrc: new FormControl(''),
    otc: new FormControl(''),
  })
})

I loop over this form using a simple Object.keys(costForm).forEach((controlName) => {...}) and then get the control with this.costForm.get(controlName) I then send over this control to various multiple function but it seems that now in one of the functions i need the controlName itself to check some conditions. Although I could send the control name to all the functions but that would be very useless in some of the functions. I have seen this fragment Get name of Form Control (answer 2) but it has too loop over the parent controls again. Is there a better way to find the name of the control?

So i currently have a reactive form with controls nested inside of it. Something like this:

costForm = new FormGroup({
  year1: new FormGroup({
    mrc: new FormControl(''),
    otc: new FormControl(''),
  })
})

I loop over this form using a simple Object.keys(costForm).forEach((controlName) => {...}) and then get the control with this.costForm.get(controlName) I then send over this control to various multiple function but it seems that now in one of the functions i need the controlName itself to check some conditions. Although I could send the control name to all the functions but that would be very useless in some of the functions. I have seen this fragment Get name of Form Control (answer 2) but it has too loop over the parent controls again. Is there a better way to find the name of the control?

Share Improve this question asked Aug 28, 2021 at 7:39 kzoepskzoeps 4717 silver badges17 bronze badges 1
  • I updated answer – user16695846 Commented Aug 28, 2021 at 8:38
Add a ment  | 

2 Answers 2

Reset to default 3

When you are using reactive form you are getting access to controls.

Object.keys(this.costForm.controls).forEach((controlName) => {
console.log(controlName)})

To get one specific control:

this.costForm.get(’nameOfControl’)

You need make a recursive function. You can think in a function that get a FomGroup or a FormArray

   loopGroup(form: FormGroup | FormArray) {
    if (form instanceof FormArray) {  //if is a FormArray
      form.controls.forEach(x => {
        if (x instanceof FormControl) this.makeSomething(x);
        if (x instanceof FormGroup || x instanceof FormArray)
         this.loopGroup(x);
      });
    } else {  //if is a FormGroup
      Object.keys(form.controls).forEach(key => {
        const control = form.get(key);
        if (control instanceof FormControl) this.makeSomething(control);
        if (control instanceof FormGroup || control instanceof FormArray)
         this.loopGroup(control);
      });
    }
  }

  makeSomething(control:AbstractControl){
      ...make something...
  }

You use as

  this.loopGroup(this.myForm)

But really I don't know want you want achieve.

Update About to get the "html tag", the problem is that a FormControl is not relationated with the html element(*). A FormControl exist independently is you has a input or not, so,e.g. you can not access to the attributes of the html tags from the FormControl

So we need take another aproach, that is create a simple directive that allow us get the NgControl and the htmlElement. Is we use some like

@Directive({
  selector: 'input'
})
export class HtmlControl{
   constructor(@Optional() private ngControl:NgControl,
               public elementRef:ElementRef){}

   isControl(control:AbstractControl)
   {
        return this.ngControl.control==control
   }
} 

We can get all the "inputs" using ViewChildren

  @ViewChildren(HtmlControl) controls:QueryList<HtmlControl>

And we can do in our function makeSomething

  makeSomething(control:AbstractControl){
    const ctrl=this.controls.find(x=>x.isControl(control))
    console.log(ctrl.elementRef)
      ...make something...
  }

(*)Really a NgControl has two properties: control and valueAccessor, in valueAccessor._elementRef you has the elementRef and in control you has the FormControl, but I don't know how access to the NgControl of a FormControl

发布评论

评论列表(0)

  1. 暂无评论