1

我正在使用 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;
}
4

2 回答 2

1

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

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

初始化选项

ngOnInit() {
   this.dateExpires = moment("2099-12-31");
}
于 2017-01-05T23:25:41.247 回答
1

你也可以像下面这样使用:

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

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

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

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

    this.datepickerOptions = new DatePickerOptions({
        format: 'DD-MM-YYYY'
    });
    
  3. 在您的 HTML 中使用它: < ng2-datepicker name="dateExpires" [options]="datepickerOptions ">

于 2017-04-14T09:53:21.033 回答