ERROR Error: No value accessor for form control with unspecified name attribute on switch

后端 未结 20 2400
感情败类
感情败类 2020-12-07 15:33

Here is my component in Angular 4:

@Component( {
    selector: \'input-extra-field\',
    template: `
            
相关标签:
20条回答
  • 2020-12-07 16:01

    I was facing this issue while running unit tests. To fix I added the MatSlideToggleModule to the imports in my spect.ts file.

    0 讨论(0)
  • 2020-12-07 16:02

    I fixed this error by adding the name="fieldName" ngDefaultControl attributes to the element that carries the [(ngModel)] attribute.

    0 讨论(0)
  • 2020-12-07 16:02

    I also had the same error , angular 7

     <button (click)="Addcity(city.name)" [(ngModel)]="city.name"  class="dropdown-item fontstyle"
         *ngFor="let city of Cities; let i = index">
          {{city.name}}
     </button>
    

    I just added ngDefaultControl

       <button (click)="Addcity(city.name)" [(ngModel)]="city.name"  ngDefaultControl class="dropdown-item fontstyle"
     *ngFor="let city of Cities; let i = index">
      {{city.name}}
    

    0 讨论(0)
  • 2020-12-07 16:04

    I also received this error while writing a custom form control component in Angular 7. However, none of the answers are applicable to Angular 7.

    In my case, the following needed to be add to the @Component decorator:

      providers: [
        {
          provide: NG_VALUE_ACCESSOR,
          useExisting: forwardRef(() => MyCustomComponent),  // replace name as appropriate
          multi: true
        }
      ]
    

    This is a case of "I don't know why it works, but it does." Chalk it up to poor design/implementation on the part of Angular.

    0 讨论(0)
  • 2020-12-07 16:04

    Have you tried moving your [(ngModel)] to the div instead of the switch in your HTML? I had the same error appear in my code and it was because I bound the model to a <mat-option> instead of a <mat-select>. Though I am not using form control.

    0 讨论(0)
  • 2020-12-07 16:09

    In my case it was a component.member which was not existing e.g.

    [formControl]="personId"
    

    Adding it to the class declaration fixed it

    this.personId = new FormControl(...)
    
    0 讨论(0)
提交回复
热议问题