CheckboxRequiredValidator

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

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

@Directive({ selector: 'input[type=checkbox][required][formControlName],input[type=checkbox][required][formControl],input[type=checkbox][required][ngModel]', providers: [CHECKBOX_REQUIRED_VALIDATOR], host: { '[attr.required]': 'required ? "" : null' } }) class CheckboxRequiredValidator extends RequiredValidator { validate(c: AbstractControl): ValidationErrors | null // 继承自 forms/RequiredValidator required: boolean | string validate(c: AbstractControl): ValidationErrors | null registerOnValidatorChange(fn: () => void): void }

选择器

input[type=checkbox][required][formControlName] input[type=checkbox][required][formControl] input[type=checkbox][required][ngModel]

说明

Example

例子

<input type="checkbox" name="active" ngModel required>

方法

validate(c: AbstractControl): ValidationErrors | null

参数

c

Type: AbstractControl.

返回值

ValidationErrors | null