【问题标题】:Error: "formControlName must be used with a parent formGroup directive." for <input ngbDatepicker #e="ngbDatepicker" (click)="e.toggle()">错误:“formControlName 必须与父 formGroup 指令一起使用。”对于 <input ngbDatepicker #e="ngbDatepicker" (click)="e.toggle()">
【发布时间】:2019-09-16 16:31:44
【问题描述】:

我有一个日期选择器,其输入字段和弹出窗口的配置如下所述: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

【问题讨论】:

  • 您可以在使用日期选择器的地方编辑和添加完整的模板吗?
  • @HDJEMAI 它不再被复制,因为我已经从那里删除了 formControlName="completed" 并且只剩下 [(ngModel)]

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


【解决方案1】:

它不再被复制,因为我从那里删除了 formControlName="completed" 并且只剩下 [(ngModel)]

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 2018-01-31
    • 2018-03-09
    • 2020-04-07
    • 2021-04-17
    • 2020-02-06
    • 2017-09-04
    • 2019-09-28
    • 2020-01-25
    相关资源
    最近更新 更多