Angular 2 Material Datepicker Value

后端 未结 3 1719
说谎
说谎 2021-01-04 21:06

I want to use Angular Material Datepicker to get a date into my page. I use this code, but cannot figure out how to access the selected value correctly.



        
相关标签:
3条回答
  • 2021-01-04 21:18

    as stated in the docs there are 2 events (dateChange) and (dateInput) that can be used if you prefer. the $event has 3 props, target is the MatDatepickerInput, targetElement for the native HTML Element, and value which is the Date object.

    <input matInput [matDatepicker]="pickerFrom" placeholder="From" 
          (dateChange)="changeFunc($event)"      <<---- you can send $event
          (dateChange)="dateInput($event.value)" <<---- or just $event.value 
    >
    
    0 讨论(0)
  • 2021-01-04 21:19

    As Nehal said, you can use the [(ngModel)] binding; I also forgot the "name" atribute:

      <md-form-field>
        <input mdInput [(ngModel)]="myDateValue" name="myDate"
          [mdDatepicker]="picker" placeholder="Select a date">
    
    0 讨论(0)
  • 2021-01-04 21:26

    You can access the datepicker value by using ngModel. The ngModel needs to be in the input tag. See the Plunker demo.

    0 讨论(0)
提交回复
热议问题