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

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

Here is my component in Angular 4:

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

    I had the same error, but in my case apparently it was a synchronization issue, at the moment of render the components html.

    I followed some of the solutions proposed on this page but any of them worked for me, at least not completely.

    What did actually solve my error was to write the below code snippet inside the father html tag of the elements .

    I was binding to the variable.

    Code:

        *ngIf="variable-name"
    

    The error was caused, apparently by the project trying to render the page, apparently at the moment of evaluating the variable, the project just could no find its value. With the above code snippet you make sure that before rendering the page you ask if the variable has being initialized.

    This is my component.ts code:

    import { Component, OnInit } from '@angular/core';
    import { InvitationService } from 'src/app/service/invitation.service';
    import { BusinessService } from 'src/app/service/business.service';
    import { Invitation } from 'src/app/_models/invitation';
    import { forEach } from '@angular/router/src/utils/collection';
    
    @Component({
      selector: 'app-invitation-details',
      templateUrl: './invitation-details.component.html',
      styleUrls: ['./invitation-details.component.scss']
    })
    export class InvitationDetailsComponent implements OnInit {
      invitationsList: any;
      currentInvitation: any;
      business: any;
      invitationId: number;
      invitation: Invitation;
    
      constructor(private InvitationService: InvitationService, private BusinessService: 
    BusinessService) { 
    this.invitationId = 1; //prueba temporal con invitacion 1
    this.getInvitations();
    this.getInvitationById(this.invitationId);
      }
    
       ngOnInit() {
    
      }
    
      getInvitations() {
        this.InvitationService.getAllInvitation().subscribe(result => {
          this.invitationsList = result;
          console.log(result);
        }, error => {
          console.log(JSON.stringify(error));
        });
      }
    
      getInvitationById(invitationId:number){
        this.InvitationService.getInvitationById(invitationId).subscribe(result => {
          this.currentInvitation = result;
          console.log(result);
          //this.business=this.currentInvitation['business'];
           //console.log(this.currentInvitation['business']); 
         }, error => {
         console.log(JSON.stringify(error));
        });
      }
          ...
    

    Here is my html markup:

    <div class="container-fluid mt--7">
      <div class="row">
        <div class="container col-xl-10 col-lg-8">
          <div class="card card-stats ">
            <div class="card-body container-fluid form-check-inline" 
             *ngIf="currentInvitation">
              <div class="col-4">
                 ...
    

    I hope this can be helpful.

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

    in my case, I had a <TEXTAREA> tag from old html while converting to angular. Had to change to <textarea>.

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

    In my case, it's happening in my shared module and I had to add the following into @NgModule:

    ...
      imports: [
        CommonModule,
        FormsModule,
        ReactiveFormsModule,
        IonicModule
      ],
    ...
    
    0 讨论(0)
  • 2020-12-07 15:50

    In my case it was as daft as having registered the new component for DI in my app.module.ts declarations but not in exports.

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

    In my case, I had inserted [(ngModel)] on label rather than input. There is also a caveat, I tried running after correctly the above error in the specified line but the error wouldn't go. If there are other places where you have committed the same mistake, it still throws you the same error at the same line

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

    I had this same error, I had a input field named control in my custom Form Component but was accidentally passing control in input named formControl. Hope no one faces that issue.

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