如何让ng2-bootstrap datepicker隐藏起来

时间:2016-07-05 12:15:38

标签: angular ng2-bootstrap

我使用ng2-bootstrap日期选择器,当我点击datepicker时,它仍然显示,如何隐藏它?我的html代码在下面。请帮助我,谢谢

<input type="text" [ngModel]="dt.toLocaleDateString()" (focus)="showDatePicker = true" style="width:300px;">
<div *ngIf="showDatePicker" style="position: absolute; z-index:10; min-height:290px;">
    <datepicker [(ngModel)]="dt"  [showWeeks]="true" ></datepicker>
</div>

3 个答案:

答案 0 :(得分:0)

我有同样的问题。目前它接缝是不可能的。作者正在研究新版本。请参阅回购:HERE

答案 1 :(得分:0)

您可以使用类名和ID来关闭datepicket弹出窗口(即DatePickerPopupDirective),使用事件目标(click)=“close($ event.target)”并且它适用于我。

在我的datepicker.component.html模板中:

  <div *ngIf="opened" class="dateclass">
<datepicker  [(ngModel)]="dt" [minDate]="minDate" [formatDayHeader]="E"   
    (click)="close($event.target)"
    [showWeeks]="false"></datepicker>
</div>

由于ng2-bootstrap datepicker使用“btn btn-default btn-sm”作为日期和年份的类名,我们在if条件下使用id和classname。

public close(e:HTMLElement): void {
    if(e.className == 'btn btn-default btn-sm' && e.id !== 'undefined-title'){
    this.opened = false;
    }
}

我的整个日期选择器组件如下所示:

import { Component, OnInit, Input } from '@angular/core';
import {CORE_DIRECTIVES} from '@angular/common';
import {DATEPICKER_DIRECTIVES} from 'ng2-bootstrap/ng2-bootstrap';
import { FORM_DIRECTIVES }from '@angular/forms';

@Component({
  moduleId: module.id,
  selector: 'app-datepicker',
  templateUrl: 'datepicker.component.html',
  directives: [DATEPICKER_DIRECTIVES, FORM_DIRECTIVES, CORE_DIRECTIVES],
  styleUrls: ['datepicker.component.css'],
  exportAs: 'dateref'
})
export class DatepickerComponent implements OnInit {
  @Input()  private dt: Date = new Date();
  public minDate: Date = void 0;
  public events: Array<any>;
  private opened: boolean = false;
  constructor() {
    this.minDate = new Date();
  }
  public getDate(): any {
   return (this.dt && this.dt.getTime());
  }

  public open(): void {
    this.opened = !this.opened;
  }

  public close(e:HTMLElement): void {
    if(e.className == 'btn btn-default btn-sm' && e.id !== 'undefined-title'){
    this.opened = false;
    }
  }
 ngOnInit() {
 }
}

在我的父模板中,我使用了exportAs'dateref'来引用@ViewChild('dp')datePicker;为:

<app-datepicker #dp="dateref"></app-datepicker>

答案 2 :(得分:-1)

这是我的代码,它运行正常:

<div class="input-group">
    <input type="text" class="form-control "
           (focus)="showDatePicker = !showDatePicker" name="inputBeginDate"
           value="{{dt | date:'MM/dd/yy'}}"/>
    <span class="input-group-addon" dropdownToggle
          (click)="showDatePicker = !showDatePicker"><i class="fa fa-calendar"></i></span>
    <div *ngIf="showDatePicker"
         style="position: absolute; z-index:10; min-height:290px;">
        <datepicker name="date" [showWeeks]="false" [(ngModel)]="dt" (ngModelChange)="setDate(dt);getInvoices(invoiceFilters)" (selectionDone)="showDatePicker = false"></datepicker>
    </div>
</div>