What causes the “control.registerOnChange is not a function” error

后端 未结 10 1911
一向
一向 2020-12-01 20:39

I have a form using the reactive form approach. The form is created as follow in my pug:

form([formGroup]=\'form\', novalidate=\'\', (ngSubmit)=\'postSurvey(         


        
相关标签:
10条回答
  • 2020-12-01 20:56

    Yes, that error message is a bit cryptic, but if you use FormBuilder, you would see this when you added a control to FormGroup in your component and named it "A", but then either forgot to add input with formControlName="A" to your template, or formControlName for the intended input is not A, or empty, or not present.

    Basically, it says: "I cannot match the control I have in FormGroup to the control in the template".

    0 讨论(0)
  • 2020-12-01 20:56

    to me it happened when I used same [formControl]="carBrand" and [matAutocomplete]="carBrandAuto" from my autocomplete input

    I changed this

    FROM:

    ...
    <input
      [formControl]="carBrand"
      [matAutocomplete]="carBrand"
    >
    <mat-autocomplete matAutocomplete #carBrand="matAutocomplete">
    ...
    

    TO

    ...
    <input
      [formControl]="carBrand"
      [matAutocomplete]="carBrandAuto"
    >
    <mat-autocomplete matAutocomplete #carBrandAuto="matAutocomplete">
    ...
    
    0 讨论(0)
  • 2020-12-01 20:59

    I came across looking for a solution to the similar issue and then found a solution myself. My issue was the following. I had a form like this

    form: FormGroup = new FormGroup({
      status: new FormArray([])
    });
    

    Initially it was represented by the list of checkboxes for each status on the template. And then I created a custom component to represent status selector and used it in template like so

    <status-selector [formControlName]="'status'"></status-selector>
    

    The problem is that formControlName must point to FormControl instance, but actually it was pointing to a FormArray instance. So, changing to status: new FormControl([]) fixed this issue for me.

    0 讨论(0)
  • 2020-12-01 21:05

    Maybe you have moved a control element outside the group in the template.

    OK:

    <div formGroupName="passwordForm">
         Password: <input type="password" formControlName="password">
         Confirm: <input type="password" formControlName="confirmPassword">
    </div>
    

    Not OK:

    Password: <input type="password" formControlName="password">
    <div formGroupName="passwordForm">
         Confirm: <input type="password" formControlName="confirmPassword">
    </div>
    
    0 讨论(0)
  • 2020-12-01 21:06

    In my case the error occurred when the formControl name was same as a template variable on the page. For example

    <select id="status" [formControl]="userStatus">...</select>
    
    <form-status #userStatus ></form-status> //matching template variable name
    
    0 讨论(0)
  • 2020-12-01 21:07

    In my case this error was thrown because I was using FormControlName instead of FormArrayName to bind to a FormArray in my template.

    My component code:

    public form = new FormGroup({
       ...
       checkboxes: new FormArray([....])
    })
    

    My template code that threw error:

    <input type="checkbox" formControlName="checkboxes" />
    

    Fix:

    <input type="checkbox" formArrayName="checkboxes" />
    
    0 讨论(0)
提交回复
热议问题