3

I want to set value of angular material datepicker, but from input outside material datepicker input

Please see this for more information: angular-material-datepicker

<mat-form-field>
    <input matInput placeholder="set date picker on blur 
    (blur)="setDatepicker()">
</mat-form-field> <br>
<mat-form-field class="example-full-width">
    <input matInput  [matDatepicker]="picker" placeholder="Choose a date">
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

2 Answers 2

4

You need update

HTML

(blur)="setDatepicker($event.target.value)">

<input matInput [(ngModel)]="input" [matDatepicker]="picker" placeholder="Choose a date">

TS

export class DatepickerFilterExample {
  input: any;
  setDatepicker(val) {
    //alert(val)
    this.input = new Date(val);
  }
}

Forked https://stackblitz.com/edit/angular-material-datepicker-example-2

Sign up to request clarification or add additional context in comments.

Comments

4

By using formGroup and HTML datepicker

import { FormGroup,FormControl} from '@angular/forms';
date;
  testForm : FormGroup;

  constructor(){
     this.testForm = new FormGroup({
          date:new FormControl(this.date),
          setDate:new FormControl(),
        })
  }
  setDatepicker(evt) {
    var value= this.testForm.controls['setDate'].value
    this.date=new Date(new Date(value).getTime())
    this.testForm.controls['date'].setValue(this.date);
  }
 <form  [formGroup]="testForm" >
<mat-form-field>
  <input matInput type="date" placeholder=" date picker on blur" (blur)="setDatepicker($event)" formControlName="setDate">
</mat-form-field> <br>
<mat-form-field class="mr-sm-24" fxFlex (click)="open()" >
   <input matInput [matDatepicker]="picker" placeholder="Date"
                  autocomplete="off"
                  name="date" formControlName="date">
   <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
   <mat-datepicker  [startAt]="date" #picker></mat-datepicker>
</mat-form-field>
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.