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
markAllAsTouched(): 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 | (string | number)[]): any
hasError(errorCode: string, path?: string | (string | number)[]): 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 | Declared in constructor. 用于决定该控件有效性的同步函数。 The function that determines the synchronous validity of this control. |
asyncValidator: AsyncValidatorFn | null | Declared in constructor. 用于决定该控件有效性的异步函数。 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 被禁用的控件会豁免有效性检查,并且它的值不会聚合进其祖先控件中。 Disabled controls are exempt from validation checks and are not included in the aggregate value of their ancestor controls. 参见: |
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. It also emits an event each time you call enable() or disable() without passing along {emitEvent: false} as a function argument. |
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. | |||
参数
返回值
| |||
When you add or remove a validator at run time, you must call |
设置该控件上所激活的异步验证器。调用它就会覆盖所有现存的异步验证器。 Sets the async validators that are active on this control. Calling this overwrites any existing async validators. | |||
参数
返回值
| |||
When you add or remove a validator at run time, you must call |
清空同步验证器列表。 Empties out the sync validator list. |
参数没有参数。 返回值
|
When you add or remove a validator at run time, you must call |
清空异步验证器列表。 Empties out the async validator list. |
参数没有参数。 返回值
|
When you add or remove a validator at run time, you must call |
Marks the control and all its descendant controls as See also:
|
参数没有参数。 返回值
|
把该控件标记为 Marks the control as See also:
| |||
参数
返回值
| |||
如果该控件有任何子控件,还会把所有子控件标记为 If the control has any children, also marks all children as |
把控件标记为 Marks the control as See also:
| |||
参数
返回值
|
把该控件标记为 Marks the control as See also:
| |||
参数
返回值
| |||
如果该控件有任何子控件,则把所有子控件标记为 If the control has any children, marks all children as |
把该控件标记为 Marks the control as See also: | |||
参数
返回值
| |||
当控件正在执行异步验证时,该控件是 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 See also: | |||
参数
返回值
| |||
如果该控件有子控件,则所有子控件也会被禁用。 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. See also: | |||
参数
返回值
| |||
默认情况下,如果该控件具有子控件,则所有子控件都会被启用。 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 the control with the given path. | ||||||
参数
返回值特定错误的数据,如果该控件不存在或没有错误,则返回 null。
| ||||||
使用说明比如,对于下列 For example, for the following
从根表单移动到这个 'street' 控件的路径应该是 'address' -> 'street'。 The path to the 'street' control from the root form would be 'address' -> 'street'. 可以用两种格式把它提供给此方法: It can be provided to this method in one of two formats:
|
报告指定路径下的控件上是否有指定的错误。 Reports whether the control with the given path has the error specified. | ||||||
参数
返回值
如果指定路径下的控件有这个错误则返回 If the control is not present, false is returned. | ||||||
使用说明For example, for the following
The path to the 'street' control from the root form would be 'address' -> 'street'. It can be provided to this method in one of two formats:
要检查的控件的路径。如果没有提供该参数,则检查该控件中的错误。 If no path is given, this method checks for the error on the current control. |