错误:“ formControlName必须与父formGroup指令一起使用。”对于<input ngbDatepicker#e =“ ngbDatepicker”(click)=“ e.toggle()”>

时间:2019-09-16 16:31:44

标签: angular typescript datepicker bootstrap-datepicker ngb-datepicker

我有一个日期选择器,其中的输入字段和弹出窗口的配置如此处所述: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模型中获取endDate值,并采用其日/月/年,并放入我的DTO中发送给服务器

我不明白,为什么只有在模板中使用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

1 个答案:

答案 0 :(得分:0)

不再复制,因为我从那里删除了formControlName =“ completed”,只剩下[[ngModel)]