RequiredValidator

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

A directive that adds the required validator to any controls marked with the required attribute. The directive is provided with the NG_VALIDATORS multi-provider list.

参见

NgModules

选择器

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

属性

属性说明
@Input()
required: boolean | string

Tracks changes to the required attribute bound to this directive.

说明

使用模板驱动表单添加必填项验证器

Adding a required validator using template-driven forms

<input name="fullName" ngModel required>
      
      <input name="fullName" ngModel required>
    

方法

Method that validates whether the control is empty. Returns the validation result if enabled, otherwise null.

validate(control: AbstractControl): ValidationErrors | null
      
      validate(control: AbstractControl): ValidationErrors | null
    
参数
control AbstractControl
返回值

ValidationErrors | null

Registers a callback function to call when the validator inputs change.

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

The callback function

返回值

void