angular material 2 date picker auto open on focus

mehran picture mehran · Aug 25, 2017 · Viewed 20.7k times · Source

How angular material 2 date picker can be configured to be opened automatically on focus? I didn't find anything in the documentation at https://material.angular.io/components/datepicker/overview.

Current html is like this:

Answer

Pengyy picture Pengyy · Aug 25, 2017

mdDatepicker provides method open() in order to open it manually doe developers. You can invoke it at md-input's focus event. See docs(Method of MatDatepicker).

<md-input-container>
  <input mdInput [mdDatepicker]="picker" (focus)="picker.open()" placeholder="Choose a date">
  <button mdSuffix [mdDatepickerToggle]="picker"></button>
</md-input-container>
<md-datepicker #picker></md-datepicker>

Demo(included demo for opening on focus and opening in typescript)