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

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

Here is my component in Angular 4:

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

    I was facing this error while running Karma Unit Test cases Adding MatSelectModule in the imports fixes the issue

    imports: [
            HttpClientTestingModule,
            FormsModule,
            MatTableModule,
            MatSelectModule,
            NoopAnimationsModule
          ],
    
    0 讨论(0)
  • 2020-12-07 15:54

    I had the same problem and the issue was that my child component had an @input named formControl.

    So I just needed to change from:

    <my-component [formControl]="formControl"><my-component/>
    

    to:

    <my-component [control]="control"><my-component/>
    

    ts:

    @Input()
    control:FormControl;
    
    0 讨论(0)
  • 2020-12-07 15:57

    I had this same error- I accidentally binded [(ngModel)] to my mat-form-field instead of the input element.

    0 讨论(0)
  • 2020-12-07 15:58

    This is kind of stupid, but I got this error message by accidentally using [formControl] instead of [formGroup]. See here:

    WRONG

    @Component({
      selector: 'app-application-purpose',
      template: `
        <div [formControl]="formGroup"> <!-- '[formControl]' IS THE WRONG ATTRIBUTE -->
          <input formControlName="formGroupProperty" />
        </div>
      `
    })
    export class MyComponent implements OnInit {
      formGroup: FormGroup
    
      constructor(
        private formBuilder: FormBuilder
      ) { }
    
      ngOnInit() {
        this.formGroup = this.formBuilder.group({
          formGroupProperty: ''
        })
      }
    }
    

    RIGHT

    @Component({
      selector: 'app-application-purpose',
      template: `
        <div [formGroup]="formGroup"> <!-- '[formGroup]' IS THE RIGHT ATTRIBUTE -->
          <input formControlName="formGroupProperty" />
        </div>
      `
    })
    export class MyComponent implements OnInit {
      formGroup: FormGroup
    
      constructor(
        private formBuilder: FormBuilder
      ) { }
    
      ngOnInit() {
        this.formGroup = this.formBuilder.group({
          formGroupProperty: ''
        })
      }
    }
    
    0 讨论(0)
  • 2020-12-07 15:59

    In my case i used directive, but hadn't imported it in my module.ts file. Import fixed it.

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

    #Background

    • NativeScript 6.0

    In my case, the error was triggered by changing element tag from to by fault. Inside <TextView an [(ngModel)]="name". was defined.

    After removing [(ngModel)]="name" error was gone.

    0 讨论(0)
提交回复
热议问题