1

what i am trying to achieve is to set empty date field. By default it fills field with current date.

My template file

<ng-datepicker [(ngModel)]="empty_date" [options]="options1"></ng-datepicker>  

My .ts file

public options1: DatepickerOptions;
ngOnInit() {
this.options1 = {
      minYear: 2018,
      maxYear: 2100,
      displayFormat: 'DD.MM.YYYY',
      barTitleFormat: 'MMMM YYYY',
      firstCalendarDay: 1
    };
this.empty_date = {formatted: ''};
}

i also tried with this.empty_date = "";, this.empty_date = null; and displayValue: '' inside options1.

How can i set initial field value as empty?

Oxyphe
  • 11
  • 1
  • 4

3 Answers3

0

According to this issue, just initialize your model to null. In the demo that they provide, I just transform from this:

export class AppHomeComponent {
  date: Date;
  options: DatepickerOptions = {
    locale: enLocale
  };
  constructor() {
    this.date = new Date();
  }
}

To:

export class AppHomeComponent {
  date: Date;
  options: DatepickerOptions = {
    locale: enLocale
  };
  constructor() {
    this.date = null;
  }
}

And worked ok:

empty input value

0

I added a template reference on the tag <ng-datepicker> and the use reset() method on my component.

HTML: <ng-datepicker #date></ng-datepicker>

COMPONENT:

@ViewChild('date') date: ElementRef;

resetDate() {
    this.date.reset();
}
Colo Ghidini
  • 658
  • 9
  • 20
0

You can easily clear selected date using ElementRef:

<ng-datepicker [(ngModel)]="empty_date" [options]="options1" #date1></ng-datepicker>  

<button (click)="date1.displayValue=''">Clear Date</button>

OR

You can clear it from component also:

@ViewChild('date1') date1: ElementRef;

resetDate() {
    this.date1['displayValue']='';
}

kuldeeps1ngh
  • 49
  • 1
  • 8