RequiredValidator

A Directive that adds the required validator to any controls marked with the required attribute, via the NG_VALIDATORS binding.

该指令会借助 NG_VALIDATORS 绑定把 required 验证器添加到任何带 required 属性的控件上。

@Directive({ selector: ':not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]', providers: [REQUIRED_VALIDATOR], host: { '[attr.required]': 'required ? "" : null' } }) class RequiredValidator implements Validator { required: boolean | string validate(c: AbstractControl): ValidationErrors | null registerOnValidatorChange(fn: () => void): void }

选择器

:not([type=checkbox])[required][formControlName] :not([type=checkbox])[required][formControl] :not([type=checkbox])[required][ngModel]

输入参数

required 绑定到 RequiredValidator.required

说明

Example

例子

<input name="fullName" ngModel required>

属性

属性名类型说明
required

方法

validate(c: AbstractControl): ValidationErrors | null

参数

c

Type: AbstractControl.

返回值

ValidationErrors | null

registerOnValidatorChange(fn: () => void): void

参数

fn

Type: () => void.

返回值

void