【发布时间】:2018-11-03 21:51:22
【问题描述】:
您将在下面看到我的模板/ Html 表单,但是,验证器不允许我将输入作为字符串处理?你能解释一下我错过了什么吗?处理和验证明显的文本输入,但我找不到如何处理电话和时间等数字输入类型的示例。我们可以在某处使用 toString() javascript方法将它们转换为字符串吗? HTML 代码示例
<mat-form-field>
<input matInput type="text" formControlName="title" placeholder="Student Name">
<mat-error *ngIf="form.get('title').invalid">Please enter a Student Name.</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput type="tel" formControlName="telephone" placeholder="telephone">
<mat-error *ngIf="form.get(telephone).invalid">Please enter a telephone.</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput type="Time" formControlName="pickUpTime" placeholder="PickUp Time">
<mat-error *ngIf="form.get('pickUpTime').invalid">Please enter a Pickup Time.</mat-error>
</mat-form-field>
<mat-form-field>
<input matInput type="Time" formControlName="dropOffTime" placeholder="DropOff Time">
<mat-error *ngIf="form.get('dropOffTime').invalid">Please enter a Dropoff Time.</mat-error>
</mat-form-field>
Component.ts 文件配置响应式表单:
ngOnInit() {
this.form = new FormGroup({
title: new FormControl(null, {
validators: [Validators.required, Validators.minLength(3)]
}),
telephone: new FormControl(null,{
validators: [
Validators.required,
Validators.minLength(5) ]
}),
pickUpTime: new FormControl(null, {
validators: [
Validators.required,
Validators.minLength(5) ]
}),
dropOffTime: new FormControl(null, {
validators: [
Validators.required,
Validators.minLength(5) ]
})
【问题讨论】:
标签: angular forms angular6 reactive-programming angular-reactive-forms