簡體   English   中英

錯誤:“formControlName 必須與父 formGroup 指令一起使用。” 為了<input ngbDatepicker #e="ngbDatepicker" (click)="e.toggle()">

[英]Error: "formControlName must be used with a parent formGroup directive." for <input ngbDatepicker #e="ngbDatepicker" (click)="e.toggle()">

我有一個日期選擇器,輸入字段和彈出窗口配置如下:https ://ng-bootstrap.github.io/#/components/datepicker/overview

所以我有

1) 標記在模板中完成的所有需要​​的導入:

<input id="field_completed" [(ngModel)]="endDate" type="text" class="form-control" name="completed" formControlName="completed" placeholder="YYYY-MM-DD" ngbDatepicker #ended="ngbDatepicker" (click)="ended.toggle()" (dateSelect)="addEndDateAutomatically()"/>

2)有處理函數,它從NgbDateStruct的模型中NgbDateStruct endDate 值,獲取它的日/月/年並將其放入我的 DTO 以發送到服務器

我無法理解,為什么只有在模板中使用了ngbDatepicker時才會出現以下錯誤,而在排除它ngbDatepicker不會:

main.99b988e9722c4de65a1a.bundle.js:1 ERROR Error: formControlName must be used with a parent formGroup directive.  You'll want to add a formGroup
       directive and pass it an existing FormGroup instance (you can create one in your class).

      Example:


    <div [formGroup]="myGroup">
      <input formControlName="firstName">
    </div>

    In your class:

    this.myGroup = new FormGroup({
       firstName: new FormControl()
    });
    at Function.l.controlParentException (main.99b988e9722c4de65a1a.bundle.js:1)
    at n._checkParentType (main.99b988e9722c4de65a1a.bundle.js:1)
    at n._setUpControl (main.99b988e9722c4de65a1a.bundle.js:1)
    at n.ngOnChanges (main.99b988e9722c4de65a1a.bundle.js:1)
    at main.99b988e9722c4de65a1a.bundle.js:1
    at main.99b988e9722c4de65a1a.bundle.js:1
    at Aw (main.99b988e9722c4de65a1a.bundle.js:1)
    at sx (main.99b988e9722c4de65a1a.bundle.js:1)
    at Object.updateDirectives (main.99b988e9722c4de65a1a.bundle.js:1)
    at Object.updateDirectives (main.99b988e9722c4de65a1a.bundle

它不再被復制,因為我已經從那里刪除了 formControlName="completed" 並且只剩下 [(ngModel)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM