admin管理员组文章数量:1279084
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>
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>
Share
Improve this question
edited May 1, 2019 at 9:22
Stepan Suvorov
26.2k26 gold badges114 silver badges178 bronze badges
asked May 1, 2019 at 5:28
JiraiyaaJiraiyaa
311 gold badge1 silver badge2 bronze badges
2 Answers
Reset to default 4You 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./edit/angular-material-datepicker-example-2
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"
autoplete="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>
本文标签: javascriptSet value angular material datepickerfrom input outsideStack Overflow
版权声明:本文标题:javascript - Set value angular material datepicker, from input outside - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741216719a2360183.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论