在ng2-datepicker上设置初始值

时间:2017-01-05 21:30:01

标签: angular typescript ng2-datepicker

我正在使用ng2-datepicker获取可用的日期和日期。我想知道是否有人知道如何设置初始值。使用日期选择器的所有内容都有效,我只希望dateAvaliable成为今天的日期,然后dateExpires成为2099-12-31

<label>Date Avaliable:</label>
<div>
  <ng2-datepicker name="dateAvaliable" [(ngModel)]="dateAvaliable" [ngModelOPtions]="{standalone: true}">  </ng2-datepicker>
</div>
<label>Date Expires:</label>
<div>
  <ng2-datepicker name="dateExpires" [(ngModel)]="dateExpires" [ngModelOPtions]="{standalone: true}">  </ng2-datepicker>
</div>

组件

private today = new Date(); 
private dd: any  = this.today.getDate(); 
private mm: any = this.today.getMonth() +1; 
private year: any = this.today.getFullYear(); 


public sop: Sop;
public dateExpires: any; 

public dateAvaliable: any;

ngOnInit() {
    if (this.dd<10) {
       this.dd ='0'+this.dd; 
    }
    if(this.mm<10){
       this.mm='0'+this.mm;
    }
    this.sop = {
      description: "",
      country: [this.countrys[4].value],
      storeType: [this.storeTypes[4].value],
      scoType: [this.scoTypes[6].value],
      audience: [this.audiences[2].value],
      sopType: [this.sopTypes[2].value],
      dateAvaliable: this.year +'-'+this.mm+'-'+this.dd,
      dateExpires: "2099-12-31"
    }
}

这是我的界面

export interface Sop {
    description: String; 
    country?: String[];
    storeType?: String[]; 
    scoType?: String[]; 
    audience?: String[]; 
    sopType?: String[];
    dateAvaliable: any; 
    dateExpires: any;
}

2 个答案:

答案 0 :(得分:1)

您需要使用[options]来设置初始值

<ng2-datepicker name="dateExpires" [(ngModel)]="dateExpires" [options]="{maxDate: dateExpires}"></ng2-datepicker>

初始化选项

ngOnInit() {
   this.dateExpires = moment("2099-12-31");
}

答案 1 :(得分:1)

你也可以使用如下:

  1. 您可以从ng2-datepicker
  2. 导入DatePickerOptions

    从'ng2-datepicker'导入{DatePickerOptions};

    1. 然后声明一个变量: public dateOptions:DateOptions;

    2. 并在ngOnit中初始化它: ngOnInit(){

      this.datepickerOptions = new DatePickerOptions({
          format: 'DD-MM-YYYY'
      });
      
    3. 在HTML中使用它: 的&LT; ng2-datepicker name =“dateExpires”[options] =“datepickerOptions”&gt;