这篇文章主要介绍了基于datepicker定义自己的angular时间组件,现在分享给大家,也给大家做个参考。
基于datepicker定义自己的angular时间组件,分享给大家。
首先是引入相应的文件jquery和datepicker,如下
"styles": [ "styles.less", "./assets/lib/datetimepicker/datetimepicker.css" ], "scripts": [ "assets/lib/jquery/jquery.min.js", "./assets/lib/datetimepicker/datetimepicker.js", ],
然后是ts文件
import { Component, EventEmitter, OnInit, AfterViewInit, ElementRef, Input, Output } from '@angular/core';
import { ControlValueAccessor, NgControl } from '@angular/forms';
declare var $: any;
@Component({
selector: 'my-datepicker',
template: '<input [name]="name" [disabled]="disabled" class="ant-input" [value]="value">'
})
export class MyDatePickerComponent implements OnInit, AfterViewInit, 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<any>();
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里面定义自己的时间格式
<my-datepicker name="jssj" [(ngModel)]="search.jssj" [options]="{format:'YYYY-MM-DD hh:mm:ss'}"></my-datepicker>上面是我整理给大家的,希望今后会对大家有帮助。
Aries Metro风格后台管理模板基于Bootstrap2.2.1制作,干净,快速,自适应屏幕分辨率大小,兼容PC端和手机移动端,帮助你建立自己的Metro应用,并节省大量的时间,它配备了巨大的收藏图标、插件和示例。
109
相关文章:
以上就是基于datepicker定义自己的angular时间组件的示例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号