2016-07-07 71 views
1

我在我的项目中使用了ng2-bootstrap日期选择器。我想在点击日历图标时显示日期选择器。还想在选择日期时将其隐藏起来。如何使用ng2-bootstrap datepicker作为弹出模式?

<label>Date:</label> 
<div class="datepickerDiv"> 
    <input type="text" value="{{ getDate() | date:'dd-MMMM-yyyy' }}" class="datepicker"> 
     <span class="cal-icon" (click)="open()"><i class="fa fa-calendar-check-o" aria-hidden="true"></i></span> 
     <span class="clearDate" (click)="clearDate()"><i class="fa fa-times-circle" aria-hidden="true"></i></span> 
     <ul class="datepicker-ul" role="menu" *ngIf="opened"> 
      <datepicker [(ngModel)]="Date" [minDate]="minDate" [showWeeks]="false"></datepicker> 
     </ul> 
</div> 

我的成分如下

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

public getDate():number { 
    return this.Date && this.Date.getTime() || new Date().getTime(); 
} 

private clearDate() { 
    this.Date = null; 
} 

现在的问题是它只会当我再次点击图标关闭。

回答

3

只需在两个函数中分开“打开”和“关闭”(现在您的“打开”实际上是“切换”)。然后使用(尚未记录)事件selectionDone($event)关闭您的日期选择器。

<datepicker [(ngModel)]="dt" [minDate]="minDate" [showWeeks]="false" (selectionDone)="onSelectionDone($event)"></datepicker> 

并在代码:

public onSelectionDone(a) { 
    this.close(); 
}