繁体   English   中英

绑定mydatepicker angular2

[英]Binding mydatepicker angular2

我正在使用MydatePicker显示日期

 <my-date-picker [options]="myDatePickerOptions"
[(ngModel)]="startDate">
 </my-date-picker>

组件为:

 private myDatePickerOptions: IMyOptions = {
        dateFormat: 'mm/dd/yyyy',
    };

我想将我从数据库获取的值绑定到此控件。 我可以将startdate获取为字符串或日期时间,但是两种方式都无法将其绑定到startDate元素。 我怎样才能做到这一点。

从文档中,

import {IMyOptions} from 'mydatepicker';

export class MyTestApp {

    private myDatePickerOptions: IMyOptions = {
        dateFormat: 'dd/mm/yyyy',
    };.
    private startDate: Object = { date: { year: 2008, month: 01, day: 01 }    };
}

您的模板应为:

<form #myForm="ngForm" novalidate>
    <my-date-picker name="mydate" [options]="myDatePickerOptions"
                    [(ngModel)]="startDate" required></my-date-picker>
</form>

我面临着同样的问题,因此在尝试了许多解决方案之后,这是最适合我的解决方案。 您的html应该是这样的,

 <my-date-picker name="LeaveFrom" [(ngModel)]="model.LeaveFrom [options]="myDatePickerOptions"></my-date-picker>

在您的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' ,
  };

现在,在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')};

确保您的模型类型中的Fromdate应该是any ,如果它是日期类型,那么它将给出IMyday的转换错误。

export interface IAddEmployeeLeave {
    LeaveFrom: any;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM