Cannot find control with name: formControlName in angular reactive form

前端 未结 6 996
無奈伤痛
無奈伤痛 2020-12-25 11:47

I found this problem in many questions in stackoverflow but no luck. Please help me for figuring out what I am doing wrong.

In component :

相关标签:
6条回答
  • 2020-12-25 12:13

    you're missing group nested controls with formGroupName directive

    <div class="panel-body" formGroupName="address">
      <div class="form-group">
        <label for="address" class="col-sm-3 control-label">Business Address</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" formControlName="street" placeholder="Business Address">
        </div>
      </div>
      <div class="form-group">
        <label for="website" class="col-sm-3 control-label">Website</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" formControlName="website" placeholder="website">
        </div>
      </div>
      <div class="form-group">
        <label for="telephone" class="col-sm-3 control-label">Telephone</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" formControlName="mobile" placeholder="telephone">
        </div>
      </div>
      <div class="form-group">
        <label for="email" class="col-sm-3 control-label">Email</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" formControlName="email" placeholder="email">
        </div>
      </div>
      <div class="form-group">
        <label for="page id" class="col-sm-3 control-label">Facebook Page ID</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" formControlName="pageId" placeholder="facebook page id">
        </div>
      </div>
      <div class="form-group">
        <label for="about" class="col-sm-3  control-label"></label>
        <div class="col-sm-3">
          <!--span class="btn btn-success form-control" (click)="openGeneralPanel()">Back</span-->
        </div>
        <label for="about" class="col-sm-2  control-label"></label>
        <div class="col-sm-3">
          <button class="btn btn-success form-control" [disabled]="companyCreatForm.invalid" (click)="openContactInfo()">Continue</button>
        </div>
      </div>
    </div>
    
    0 讨论(0)
  • 2020-12-25 12:15

    I also had this error, and you helped me solve it. If formGroup or formGroupName are not written with the good case, then the name of the control is not found. Correct the case of formGroup or formGroupName and it is OK.

    0 讨论(0)
  • 2020-12-25 12:15

    For me even with [formGroup] the error was popping up "Cannot find control with name:''".
    It got fixed when I added ngModel Value to the input box along with formControlName="fileName"

      <form class="upload-form" [formGroup]="UploadForm">
      <div class="row">
        <div class="form-group col-sm-6">
          <label for="fileName">File Name</label>
          <!-- *** *** *** Adding [(ngModel)]="FileName" fixed the issue-->
          <input type="text" class="form-control" id="fileName" [(ngModel)]="FileName"
            placeholder="Enter file name" formControlName="fileName"> 
        </div>
        <div class="form-group col-sm-6">
          <label for="selectedType">File Type</label>
          <select class="form-control" formControlName="selectedType" id="selectedType" 
            (change)="TypeChanged(selectedType)" name="selectedType" disabled="true">
            <option>Type 1</option>
            <option>Type 2</option>
          </select>
        </div>
      </div>
      <div class="form-group">
        <label for="fileUploader">Select {{selectedType}} file</label>
        <input type="file" class="form-control-file" id="fileUploader" (change)="onFileSelected($event)">
      </div>
      <div class="w-80 text-right mt-3">
        <button class="btn btn-primary mb-2 search-button cancel-button" (click)="cancelUpload()">Cancel</button>
        <button class="btn btn-primary mb-2 search-button" (click)="uploadFrmwrFile()">Upload</button>
      </div>
     </form>
    

    And in the controller

    ngOnInit() {
    this.UploadForm= new FormGroup({
      fileName: new FormControl({value: this.FileName}),
      selectedType: new FormControl({value: this.selectedType, disabled: true}, Validators.required),
      frmwareFile: new FormControl({value: ['']})
    });
    }
    
    0 讨论(0)
  • 2020-12-25 12:21

    I tried to generate a form dynamically because the amount of questions depend on an object and for me the error was fixed when I added ngDefaultControl to my mat-form-field.

        <form [formGroup]="questionsForm">
            <ng-container *ngFor="let question of questions">
                <mat-form-field [formControlName]="question.id" ngDefaultControl>
                    <mat-label>{{question.questionContent}}</mat-label>
                    <textarea matInput rows="3" required></textarea>
                </mat-form-field>
            </ng-container>
            <button mat-raised-button (click)="sendFeedback()">Submit all questions</button>
        </form>
    

    In sendFeedback() I get the value from my dynamic form by selecting the formgroup's value as such

      sendFeedbackAsAgent():void {
        if (this.questionsForm.valid) {
          console.log(this.questionsForm.value)
        }
      }
    
    0 讨论(0)
  • 2020-12-25 12:24

    In your HTML code

    <form [formGroup]="userForm">
        <input type="text" class="form-control"  [value]="item.UserFirstName" formControlName="UserFirstName">
        <input type="text" class="form-control"  [value]="item.UserLastName" formControlName="UserLastName">
    </form>
    

    In your Typescript code

    export class UserprofileComponent implements OnInit {
        userForm: FormGroup;
        constructor(){ 
           this.userForm = new FormGroup({
              UserFirstName: new FormControl(),
              UserLastName: new FormControl()
           });
        }
    }
    

    This works perfectly, it does not give any error.

    0 讨论(0)
  • 2020-12-25 12:27

    You should specify formGroupName for nested controls

    <div class="panel panel-default" formGroupName="address"> <== add this
        <div class="panel-heading">Contact Info</div>
    

    Plunker Example

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