您可以使用類名稱和ID使用事件目標(click)=「close($ event.target)」關閉datepicket彈出窗口(即DatePickerPopupDirective),它適用於我。
在我datepicker.component.html模板:
<div *ngIf="opened" class="dateclass">
<datepicker [(ngModel)]="dt" [minDate]="minDate" [formatDayHeader]="E"
(click)="close($event.target)"
[showWeeks]="false"></datepicker>
</div>
因爲NG2的自舉日期選擇器採用「BTN BTN-默認BTN-SM」作爲類名日期和一年中,我們使用的ID和類名中,如果條件。
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')日期選擇器;如:
<app-datepicker #dp="dateref"></app-datepicker>
你應該,在一般情況下,儘量提供_why_你的代碼的作品是什麼提問者應以不同的方式來做解釋和讓自己的代碼工作,而不是隻提供一個片段。 –