kekeh / angular-mydatepicker

Angular datepicker and date range picker :date:
https://kekeh.github.io/angular-mydatepicker/
MIT License
1 stars 11 forks source link

Default value in reactive form #45

Closed RonRofe closed 4 years ago

RonRofe commented 4 years ago

How can I put a default value on reactive forms when the form initializes ?

kekeh commented 4 years ago

There is an example in wiki pages. Example app example.

Both of those examples use javascript date object to initialize the date. It is also possible to initialize with the IMyDate object.

Example to initialize with IMyDate object.

<input class="form-control" placeholder="Select a date" angular-mydatepicker 
    name="myDate" formControlName="myDate" [options]="myOptions" 
    #dp="angular-mydatepicker"/>
ngOnInit() {
  // Initialize to yesterday date
  let d: Date = new Date();
  d.setDate(d.getDate() - 1);

  let model: IMyDateModel = {isRange: false, singleDate: {
     date: { year: d.getFullYear(), month: d.getMonth() + 1, day: d.getDate() }
  }};

  this.myForm = this.formBuilder.group({
    myDate: [model, Validators.required]
    // other controls are here...
  });
}