基于datepicker定义自己的angular时间组件,分享给大家。
首先是引入相应的文件jquery和datepicker,如下
然后是ts文件
@Component({
selector: 'my-datepicker',template: '<input [name]="name" [disabled]="disabled" class="ant-input" [value]="value">'
})
export class MyDatePickerComponent implements OnInit,ControlValueAccessor {
constructor(
private _element: ElementRef,public _control: NgControl
) {
if (this._control) {
this._control.valueAccessor = this;
}
}
@Input()
name:string;
@Input()
disabled:string;
@Input()
options:Object = {};
@Input('ngModel')
value: string;
@Output() onChoose = new EventEmitter
defaults: Object;
_onChange = (value: any) => {};
writeValue(value: string) {
if (value) {
this.value = value;
}
}
registerOnChange(fn: (value: any) => void) {
this._onChange = fn;
}
registerOnTouched(fn: any) {
}
ngOnInit() {
if (this.value == undefined) {
this.value = '';
}
let _this = this;
this.defaults = {
format: 'YYYY-MM-DD',isToday:true,choosefun: function(ele,data){
_this._choose(data);
},clearfun: function(){
_this._clear();
},closefun: function() {
_this._close();
}
};
}
ngAfterViewInit() {
let options = $.extend({},this.defaults,this.options);
$(this._element.nativeElement).find('input').jeDate(options)
.on('click',function(e) {
e.stopPropagation();
$(this).addClass('focus').blur();
});
}
private _choose(value: string) {
this._onChange(value);
this.onChoose.emit(value); // 选中事件
}
private _clear() {
this._onChange('');
this.onChoose.emit(''); // 选中事件
}
private _close() {
$(this._element.nativeElement).find('input').removeClass('focus');
}
}
最后是调用,option里面定义自己的时间格式