[英]Get value out of mat-datepicker in FormControl
我正在创建对 API 的 POST 请求,其中一个引发错误的字段是mat-datepicker
字段,因为它位于ngOnInit()
调用中(我猜还没有选择任何内容)。 诸如name
、 email
等字段表现良好,但我无法从 datepicker 字段中获取值...如何在提交表单时也发送该值?
我的尝试是这样的:
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>
在 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
])
});
安慰:
get(){
console.log(this.empForm.get('date_of_birth').value);
}
希望这会帮助你。 如果有任何问题,那么我认为它与您拥有的自定义验证器有关 - this.backendValidation.bind(this, 'date_of_birth')
。
希望这有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.