AbstractControl
这是 FormControl
、FormGroup
和 FormArray
的基类。
This is the base class for FormControl
, FormGroup
, and FormArray
.
abstract class AbstractControl {
constructor(validator: ValidatorFn, asyncValidator: AsyncValidatorFn)
value: any
validator: ValidatorFn | null
asyncValidator: AsyncValidatorFn | null
parent: FormGroup | FormArray
status: string
valid: boolean
invalid: boolean
pending: boolean
disabled: boolean
enabled: boolean
errors: ValidationErrors | null
pristine: boolean
dirty: boolean
touched: boolean
untouched: boolean
valueChanges: Observable<any>
statusChanges: Observable<any>
updateOn: FormHooks
root: AbstractControl
setValidators(newValidator: ValidatorFn | ValidatorFn[]): void
setAsyncValidators(newValidator: AsyncValidatorFn | AsyncValidatorFn[]): void
clearValidators(): void
clearAsyncValidators(): void
markAsTouched(opts: { onlySelf?: boolean; } = {}): void
markAsUntouched(opts: { onlySelf?: boolean; } = {}): void
markAsDirty(opts: { onlySelf?: boolean; } = {}): void
markAsPristine(opts: { onlySelf?: boolean; } = {}): void
markAsPending(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
disable(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
enable(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
setParent(parent: FormGroup | FormArray): void
abstract setValue(value: any, options?: Object): void
abstract patchValue(value: any, options?: Object): void
abstract reset(value?: any, options?: Object): void
updateValueAndValidity(opts: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
setErrors(errors: ValidationErrors, opts: { emitEvent?: boolean; } = {}): void
get(path: string | (string | number)[]): AbstractControl | null
getError(errorCode: string, path?: string[]): any
hasError(errorCode: string, path?: string[]): boolean
}
参见
说明
它提供了一些所有控件和控件组共有的行为,比如运行验证器、计算状态和重置状态。 它还定义了一些所有子类共享的属性,如 value
、valid
和 dirty
。不允许直接实例化它。
It provides some of the shared behavior that all controls and groups of controls have, like running validators, calculating status, and resetting state. It also defines the properties that are shared between all sub-classes, like value
, valid
, and dirty
. It shouldn't be instantiated directly.
构造函数
初始化这个 AbstractControl 实例。 Initialize the AbstractControl instance. | ||||||
参数
|
属性
属性 | 说明 |
---|---|
value: any | 只读 控件的当前值。 The current value of the control.
|
validator: ValidatorFn | null | 用于决定该控件有效性的同步函数。 The function that determines the synchronous validity of this control. 声明于构造函数中 |
asyncValidator: AsyncValidatorFn | null | 用于决定该控件有效性的异步函数。 The function that determines the asynchronous validity of this control. 声明于构造函数中 |
parent: FormGroup | FormArray | 只读 父控件。 The parent control. |
status: string | 只读 控件的有效性状态。有四个可能的值: The validation status of the control. There are four possible validation status values:
这些状态值是互斥的,因此一个控件不可能同时处于有效状态和无效状态或无效状态和禁用状态。 These status values are mutually exclusive, so a control cannot be both valid AND invalid or invalid AND disabled. |
valid: boolean | 只读 当控件的 A control is |
invalid: boolean | 只读 当控件的 A control is |
pending: boolean | 只读 当控件的 A control is |
disabled: boolean | 只读 当控件的 A control is |
enabled: boolean | 只读 如果控件的 A control is |
errors: ValidationErrors | null | 只读 一个对象,包含由失败的验证所生成的那些错误,如果没出错则为 null。 An object containing any errors generated by failing validation, or null if there are no errors. |
pristine: boolean | 只读 如果用户尚未修改 UI 中的值,则该控件是 A control is |
dirty: boolean | 只读 如果用户修改过 UI 中的值,则控件是 A control is |
touched: boolean | 只读 如果控件被标记为 True if the control is marked as 一旦用户在控件上触发了 A control is marked |
untouched: boolean | 只读 如果该控件尚未标记为 True if the control has not been marked as touched 如果用户尚未在控件上触发过 A control is |
valueChanges: Observable<any> | 只读 一个多播 Observable(可观察对象),每当控件的值发生变化时,它就会发出一个事件 —— 无论是通过 UI 还是通过程序。 A multicasting observable that emits an event every time the value of the control changes, in the UI or programmatically. |
statusChanges: Observable<any> | 只读 一个多播 Observable(可观察对象),每当控件的验证 A multicasting observable that emits an event every time the validation |
updateOn: FormHooks | 只读 报告这个 Reports the update strategy of the |
root: AbstractControl | 只读 获取该控件的顶级祖先。 Retrieves the top-level ancestor of this control. |
方法
设置该控件上所激活的同步验证器。调用它将会覆盖所有现存的同步验证器。 Sets the synchronous validators that are active on this control. Calling this overwrites any existing sync validators. | |||
参数
返回值
|
设置该控件上所激活的异步验证器。调用它就会覆盖所有现存的异步验证器。 Sets the async validators that are active on this control. Calling this overwrites any existing async validators. | |||
参数
返回值
|
清空同步验证器列表。 Empties out the sync validator list. |
参数没有参数。 返回值
|
清空异步验证器列表。 Empties out the async validator list. |
参数没有参数。 返回值
|
把该控件标记为 Marks the control as | |||
参数
返回值
| |||
如果该控件有任何子控件,还会把所有子控件标记为 If the control has any children, also marks all children as |
把控件标记为 Marks the control as | |||
参数
返回值
|
把该控件标记为 Marks the control as | |||
参数
返回值
| |||
如果该控件有任何子控件,则把所有子控件标记为 If the control has any children, marks all children as |
把该控件标记为 Marks the control as | |||
参数
返回值
| |||
当控件正在执行异步验证时,该控件是 A control is pending while the control performs async validation. |
禁用此控件。这意味着该控件在表单验证检查时会被豁免,并且从其父控件的聚合值中排除它的值。它的状态是 Disables the control. This means the control is exempt from validation checks and excluded from the aggregate value of any parent. Its status is | |||
参数
返回值
| |||
如果该控件有子控件,则所有子控件也会被禁用。 If the control has children, all children are also disabled. |
启用该控件。这意味着该控件包含在有效性检查中,并会出现在其父控件的聚合值中。它的状态会根据它的值和验证器而重新计算。 Enables the control. This means the control is included in validation checks and the aggregate value of its parent. Its status recalculates based on its value and its validators. | |||
参数
返回值
| |||
默认情况下,如果该控件具有子控件,则所有子控件都会被启用。 By default, if the control has children, all children are enabled. |
设置该控件的值。这是一个抽象方法(由子类实现)。 Sets the value of the control. Abstract method (implemented in sub-classes). |
修补(patch)该控件的值。这是一个抽象方法(由子类实现)。 Patches the value of the control. Abstract method (implemented in sub-classes). |
重置控件。这是一个抽象方法(由子类实现)。 Resets the control. Abstract method (implemented in sub-classes). |
重新计算控件的值和校验状态。 Recalculates the value and validation status of the control. | |||
参数
返回值
| |||
默认情况下,它还会更新其直系祖先的值和有效性状态。 By default, it also updates the value and validity of its ancestors. |
在手动(而不是自动)运行校验之后,设置表单控件上的错误信息。 Sets errors on a form control when running validations manually, rather than automatically. | ||||||
参数
返回值
| ||||||
调用 Calling | ||||||
使用说明手动设置控件上的错误信息。Manually set the errors for a control
|
根据指定的控件名称或路径获取子控件。 Retrieves a child control given the control's name or path. | |||
参数
返回值
| |||
使用说明获取嵌套的控件Retrieve a nested control比如,要获取子控件组 For example, to get a
-OR-
|
获取发生在该控件或其他控件中发生的指定错误的出错数据。 Reports error data for a specific error occurring in this control or in another control. | ||||||
参数
返回值如果指定路径下的控件具有指定的错误,则返回出错数据,否则为
|
报告指定路径下的控件上是否有指定的错误。 Reports whether the control with the given path has the error specified. | ||||||
参数
返回值如果指定路径下的控件有这个错误则返回
|