admin管理员组文章数量:1355713
I am creating a POST request to an API, and one of the fields which throws an error is a mat-datepicker
field, since it's inside the ngOnInit()
call(and nothing is yet selected, I guess). Fields such as name
, email
etc. behave well, but I'm having trouble getting a value out of the datepicker field... How could I send that value as well when submitting the form?
My attempt was this:
ngOnInit() {
this.myForm = new FormGroup({
date_of_birth: new FormControl(new Date().toISOString(), [
Validators.required,
this.backendValidation.bind(this, 'date_of_birth')
])
})
}
<mat-form-field>
<mat-label>Date</mat-label>
<input matInput [matDatepicker]="date_of_birth" formControlName="date_of_birth" />
<mat-datepicker-toggle matSuffix [for]="date_of_birth"></mat-datepicker-toggle>
<mat-datepicker #date_of_birth></mat-datepicker>
</mat-form-field>
I am creating a POST request to an API, and one of the fields which throws an error is a mat-datepicker
field, since it's inside the ngOnInit()
call(and nothing is yet selected, I guess). Fields such as name
, email
etc. behave well, but I'm having trouble getting a value out of the datepicker field... How could I send that value as well when submitting the form?
My attempt was this:
ngOnInit() {
this.myForm = new FormGroup({
date_of_birth: new FormControl(new Date().toISOString(), [
Validators.required,
this.backendValidation.bind(this, 'date_of_birth')
])
})
}
<mat-form-field>
<mat-label>Date</mat-label>
<input matInput [matDatepicker]="date_of_birth" formControlName="date_of_birth" />
<mat-datepicker-toggle matSuffix [for]="date_of_birth"></mat-datepicker-toggle>
<mat-datepicker #date_of_birth></mat-datepicker>
</mat-form-field>
Share
Improve this question
edited Mar 30, 2020 at 12:00
Smithy
asked Mar 30, 2020 at 11:27
SmithySmithy
8476 gold badges19 silver badges51 bronze badges
2
- the value must be an object date – Eliseo Commented Mar 30, 2020 at 11:53
- @Eliseo could you please show how could I implement that? – Smithy Commented Mar 30, 2020 at 11:57
1 Answer
Reset to default 5In HTML:
<form [formGroup]="empForm" (ngSubmit)="onSubmit()">
<mat-form-field>
Date
<input matInput [matDatepicker]="picker" formControlName="date_of_birth" />
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
</form>
<button (click)="get()">get</button>
TS:
this.empForm = this.fb.group({
date_of_birth: new FormControl(new Date(), [
Validators.required
])
});
To console:
get(){
console.log(this.empForm.get('date_of_birth').value);
}
Hope this will help you. If any issues, then I think its problem with the custom validator you have - this.backendValidation.bind(this, 'date_of_birth')
.
Hope this works !
本文标签: javascriptGet value out of matdatepicker in FormControlStack Overflow
版权声明:本文标题:javascript - Get value out of mat-datepicker in FormControl - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743993312a2572544.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论