【问题标题】:Cannot close ngb-datepicker with .close() (.close() is not a function)无法使用 .close() 关闭 ngb-datepicker(.close() 不是函数)
【发布时间】:2019-03-07 16:03:27
【问题描述】:

我正在尝试设置一个 ngb-datepicker 以在单击它外部时关闭。但是我收到了这个错误:

GamesComponent.html:9 ERROR TypeError: dp.close is not a function
at GamesComponent.push../src/web/apps/command-center/games/games.component.ts.GamesComponent.closeCalendar (games.component.ts:202)
at Object.eval [as handleEvent] (GamesComponent.html:12)
at handleEvent (core.js:10050)
at callWithDebugContext (core.js:11143)
at Object.debugHandleEvent [as handleEvent] (core.js:10846)
at dispatchEvent (core.js:7509)
at core.js:7953
at HTMLDocument.<anonymous> (platform-browser.js:1140)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3748)

相关的html如下

<ng-template #popContent class="col-lg-3 align-items-center">
        <ngb-datepicker (select)="convertNgbDatetoDate($event)"
                        name="dp" ngbDatepicker #d="ngbDatepicker"
                        (blur)="onInputBlur()"
                        (document:click)="closeCalendar($event, d)">
        </ngb-datepicker>
</ng-template>

和 ts

closeCalendar(event, dp) {

    console.log(dp)

    const path = event.path.map(p => p.localName);
    if ((!path.includes('ngb-datepicker') && !path.includes('ng- 
    container') && !path.includes('gns-sentient-dates') && 
    !path.includes('howser-input'))) {
        dp.close();
        this.onInputBlur();
    }
}

关于我缺少什么的任何想法?

【问题讨论】:

  • close() 仅存在于 input[ngbDatepicker] 上,而不存在于 &lt;ngb-datepicker&gt; 上。但是关闭外部点击已经在 3.2.2 版的 ng-bootstrap 中完全实现。

标签: html angular twitter-bootstrap typescript ng-bootstrap


【解决方案1】:

自 2016 年以来,通过点击外部关闭 ngb-datepicker 是 ng-bootstrap 的一个未解决问题。请查看 this issue

可能的解决方法:

A)

使用视图:

(click)="d.toggle(); $event.stopPropagation();" (document:click)="d.close()"

或 B)

使用组件:

if(!this._eref.nativeElement.querySelector('ngb-datepicker').contains(event.target)
    && !this._eref.nativeElement.querySelector('.input-group-addon').contains(event.target)) {
    let self = this;
    setTimeout(function(){
       self.dynamicId.close();
    },10);
}

或 C)

使用视图:

<input style="background-color: white;" class="form-control" placeholder="YYYY-MM-DD" name="date" [(ngModel)]="date" ngbDatepicker #eToggle="ngbDatepicker" (click)="eToggle.toggle(); sToggle.close();$event.stopPropagation()" (document:click)="decideClosure($event, eToggle)" readonly>

和组件:

decideClosure(event, datepicker) { const path = event.path.map(p => p.localName); if (!path.includes('ngb-datepicker')) { datepicker.close(); } }

【讨论】:

  • 我尝试实现 A) 我仍然收到同样的错误:GamesComponent.html:9 ERROR TypeError: jit_nodeValue_3(...).close is not a function
  • 您能否创建一个mcve(例如使用https://stackblitz.com/)以便我们运行您的代码并进行修改?谷歌“stackblitz ng-bootstrap”来找到一个模板来集成你的代码。
  • 这个项目有这么多的相互依赖关系,我不能。
  • 尝试将您的问题分解为重现它所需的主要组件和依赖项。它将帮助您找到错误。祝你好运
  • 提到的问题没有打开 - 它已在 ng-bootstrap 3.2.2 中修复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 2019-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多