我想知道为什么在Angular的响应式形式中,带有属性type='number'
的输入标签在为空时具有null
作为值,尽管在原生HTML5中我们从未看到它们具有null
作为值。
Angular响应式表单中的Input标签如下所示:
<mat-form-field appearance="outline">
<input matInput formControlName="min" type="number" />
</mat-form-field>
定义FormGroup
的函数如下所示。
function createFromGroup() {
return new FormGroup({
min: new FormControl('', /* custom validator */),
});
}
在自定义验证器中,为了捕获输入为空的情况,我必须找出它为null。
const customValidator = (): ValidatorFn => {
return (control: AbstractControl): ValidationErrors | null => {
if (control.value === null /*here*/ || control.value === '') return null; // pass validation as exception
...
...
};
};
很可能来自matInput
指令。
确定占位符的这一行:
const placeholder = this._formField?._hideControlPlaceholder?.() ? null : this.placeholder;