您可以使用类名称和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_你的代码的作品是什么提问者应以不同的方式来做解释和让自己的代码工作,而不是只提供一个片段。 –