MaxLengthValidator

该指令用于为带有 maxlength 属性的控件添加最大长度验证器。该指令会提供 NG_VALIDATORS 多重提供商列表。

A directive that adds max length validation to controls marked with the maxlength attribute. The directive is provided with the NG_VALIDATORS multi-provider list.

参见

NgModules

选择器

属性

属性说明
@Input()
maxlength: string

Tracks changes to the the maximum length bound to this directive.

说明

添加最大长度验证器

Adding a maximum length validator

下面的例子演示了如何为一个带有 ngModel 绑定的输入框添加最大长度验证器。

The following example shows how to add a maximum length validator to an input attached to an ngModel binding.

<input name="firstName" ngModel maxlength="25">
      
      <input name="firstName" ngModel maxlength="25">
    

方法

A lifecycle method called when the directive's inputs change. For internal use only.

ngOnChanges(changes: SimpleChanges): void
      
      ngOnChanges(changes: SimpleChanges): void
    
参数
changes SimpleChanges

A object of key/value pairs for the set of changed inputs.

返回值

void

Method that validates whether the value exceeds the maximum length requirement.

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