[英]Binding mydatepicker angular2
I am using MydatePicker to show dates 我正在使用MydatePicker显示日期
<my-date-picker [options]="myDatePickerOptions"
[(ngModel)]="startDate">
</my-date-picker>
Component as: 组件为:
private myDatePickerOptions: IMyOptions = {
dateFormat: 'mm/dd/yyyy',
};
I want to bind the value I am getting from database to this control. 我想将我从数据库获取的值绑定到此控件。 I can get the startdate as string or datetime but both ways I am not able to bind it to the startDate element. 我可以将startdate获取为字符串或日期时间,但是两种方式都无法将其绑定到startDate元素。 How can I do that. 我怎样才能做到这一点。
From the documentation , 从文档中,
import {IMyOptions} from 'mydatepicker';
export class MyTestApp {
private myDatePickerOptions: IMyOptions = {
dateFormat: 'dd/mm/yyyy',
};.
private startDate: Object = { date: { year: 2008, month: 01, day: 01 } };
}
And your template should be : 您的模板应为:
<form #myForm="ngForm" novalidate>
<my-date-picker name="mydate" [options]="myDatePickerOptions"
[(ngModel)]="startDate" required></my-date-picker>
</form>
I am facing the same issue,so after trying many solution, this is the simplest solution which works for me. 我面临着同样的问题,因此在尝试了许多解决方案之后,这是最适合我的解决方案。 your html should be like this, 您的html应该是这样的,
<my-date-picker name="LeaveFrom" [(ngModel)]="model.LeaveFrom [options]="myDatePickerOptions"></my-date-picker>
And in you ts file,first import IMyDpOptions form mydatepicker and interface for your model. 在您的ts文件中,首先导入IMyDpOptions,形成mydatepicker和模型接口 。
import { IMyDpOptions } from 'mydatepicker';
import { IAddEmployeeLeave,} from 'src/shared/interfaces/employeeleave';
model = IAddEmployeeLeave;
public myDatePickerOptions: IMyDpOptions = {
// other options...
dateFormat: 'dd-mm-yyyy',
disableWeekends: true,
firstDayOfWeek : 'su' ,
};
Now in ngOnInit method, you will get your date from databse through API, then bind that date in your model like this, 现在,在ngOnInit方法中,您将通过API从数据库获取日期,然后像这样将日期绑定到模型中,
// code for API call and subscribe that response here and get date from that response.
// i am taking static date here, but you can use response's date.
// you can bind other mydatetimepicker's date-type also here, but i am using jsdate here.
this.model.LeaveFrom = {jsdate: new Date('7/08/2018')};
make sure, that in your model type of Fromdate should be any , if it is in date type then it will give conversion error with IMyday . 确保您的模型类型中的Fromdate应该是any ,如果它是日期类型,那么它将给出IMyday的转换错误。
export interface IAddEmployeeLeave {
LeaveFrom: any;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.