angular - 模板驱动表单中的角度材料日期选择器验证问题
问题描述
我在 Angular 8 应用程序中以 n 模板驱动形式使用 mat-date 选择器。“必需”验证未按预期工作。我看到了一些使用反应式表单的解决方案,但找不到任何使用模板驱动表单的解决方案。这是我的代码:
<form name="actionPoint" role="form" (ngSubmit)="actionPoint.form.valid && confirmModal()"
#actionPoint="ngForm" novalidate autocomplete="off">
<mat-form-field>
<input color="primary" matInput [matDatepicker]="picker"
id="field_effectiveDate" [max]="maxDate" name="closedDate"
[(ngModel)]="actionPointClosedOnDate" #closedDate="ngModel" disabled
(dateChange)="onActionPointClosedOnDateChange($event)" required
closedDate>
<div *ngIf="actionPoint.submitted" class="invalid-feedback">
<div
*ngIf="actionPoint.controls['closedDate']?.hasError('required')">
Date is required</div>
</div>
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker [disabled]="false">
</mat-datepicker>
解决方案
写在component.ts文件中
if(this.actionPointClosedOnDate.trim() == "")
{
alert("Enter Date");
return;
}
推荐阅读
- java - 无法在有状态会话 bean 中持续存在
- machine-learning - 高斯 SVM 的理论优势
- ios - 如何将数据从数组传递到测验应用程序的多个标签?
- mysql - MySQL 按日期划分的百分比份额
- bootstrap-4 - 如何修复引导模式不透明度错误?
- mysql - 如何使用 MySQL 为 Symfony 项目设置 Docker?
- ios - 在标题集合视图中分组数据
- java - 如何按数字而不是字母顺序对文件名集合进行排序?
- python - urllib.request.urlretrieve 返回损坏的文件(如何处理这种 url?)
- python - 如何获得由像素数组和 RGB 组成的矩阵?