跟踪独立表单控件的值和验证状态。
Tracks the value and validation status of an individual form control.
查看"说明"...
class
FormControl extends
AbstractControl {
constructor (formState: any = null, validatorOrOpts?: ValidatorFn | AbstractControlOptions | ValidatorFn[], asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[]) setValue (value: any, options: { onlySelf?: boolean; emitEvent?: boolean; emitModelToViewChange?: boolean; emitViewToModelChange?: boolean; } = {}): void patchValue (value: any, options: { onlySelf?: boolean; emitEvent?: boolean; emitModelToViewChange?: boolean; emitViewToModelChange?: boolean; } = {}): void reset (formState: any = null, options: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void registerOnChange (fn: Function): void registerOnDisabledChange (fn: (isDisabled: boolean) => void): void // 继承自
forms/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 }
class FormControl extends AbstractControl {
constructor ( formState : any = null , validatorOrOpts ?: ValidatorFn | AbstractControlOptions | ValidatorFn [], asyncValidator ?: AsyncValidatorFn | AsyncValidatorFn [])
setValue ( value : any , options : { onlySelf ?: boolean ; emitEvent ?: boolean ; emitModelToViewChange ?: boolean ; emitViewToModelChange ?: boolean ; } = {}): void
patchValue ( value : any , options : { onlySelf ?: boolean ; emitEvent ?: boolean ; emitModelToViewChange ?: boolean ; emitViewToModelChange ?: boolean ; } = {}): void
reset ( formState : any = null , options : { onlySelf ?: boolean ; emitEvent ?: boolean ; } = {}): void
registerOnChange ( fn : Function ): void
registerOnDisabledChange ( fn : ( isDisabled : boolean ) => void ): void
// 继承自 forms/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
}
说明 它和 FormGroup
和 FormArray
是 Angular 表单的三大基本构造块之一。 它扩展了 AbstractControl
类,并实现了关于访问值、验证状态、用户交互和事件的大部分基本功能。
This is one of the three fundamental building blocks of Angular forms, along with FormGroup
and FormArray
. It extends the AbstractControl
class that implements most of the base functionality for accessing the value, validation status, user interactions and events.
构造函数 方法 设置该表单控件的新值。
Sets a new value for the form control.
setValue (value: any, options: { onlySelf?: boolean; emitEvent?: boolean; emitModelToViewChange?: boolean; emitViewToModelChange?: boolean; } = {}): void
setValue ( value : any , options : { onlySelf ?: boolean ; emitEvent ?: boolean ; emitModelToViewChange ?: boolean ; emitViewToModelChange ?: boolean ; } = {}): void
参数 value
any
控件的新值。
The new value for the control.
options
object
当值发生变化时,该配置项决定如何传播变更以及发出事件。 该配置项会传递给 updateValueAndValidity 方法。
Configuration options that determine how the control propagates changes and emits events when the value changes. The configuration options are passed to the updateValueAndValidity method.
onlySelf
:如果为 true
,则每次变更只影响该控件本身,不影响其父控件。默认为 false
。
onlySelf
: When true, each change only affects this control, and not its parent. Default is false.
emitEvent
:如果为 true
或未提供(默认),则当控件值变化时, statusChanges
和 valueChanges
这两个 Observable 都会以最近的状态和值发出事件。 如果为 false
,则不会发出事件。
emitEvent
: When true or not supplied (the default), both the statusChanges
and valueChanges
observables emit events with the latest status and value when the control value is updated. When false, no events are emitted.
emitModelToViewChange
:如果为 true
或未提供(默认),则每次变化都会触发一个 onChange
事件以更新视图。
emitModelToViewChange
: When true or not supplied (the default), each change triggers an onChange
event to update the view.
emitViewToModelChange
:如果为 true
或未提供(默认),则每次变化都会触发一个 ngModelChange
事件以更新模型。
emitViewToModelChange
: When true or not supplied (the default), each change triggers an ngModelChange
event to update the model.
可选. 默认值是 {}
.
返回值 void
修补控件的值。
Patches the value of a control.
See also:
setValue
的配置项
setValue
for options
patchValue (value: any, options: { onlySelf?: boolean; emitEvent?: boolean; emitModelToViewChange?: boolean; emitViewToModelChange?: boolean; } = {}): void
patchValue ( value : any , options : { onlySelf ?: boolean ; emitEvent ?: boolean ; emitModelToViewChange ?: boolean ; emitViewToModelChange ?: boolean ; } = {}): void
参数 value
any
options
object
可选. 默认值是 {}
.
返回值 void
在 FormControl
这个层次上,该函数的功能和 setValue 完全相同。 但 FormGroup
和 FormArray
上的 patchValue 则具有不同的行为。
This function is functionally the same as setValue at this level. It exists for symmetry with patchValue on FormGroups
and FormArrays
, where it does behave differently.
重置该表单控件,把它标记为 pristine
和 untouched
,并把它的值设置为 null
。
Resets the form control, marking it pristine
and untouched
, and setting the value to null.
reset (formState: any = null, options: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
reset ( formState : any = null , options : { onlySelf ?: boolean ; emitEvent ?: boolean ; } = {}): void
参数 formState
any
使用初始值或一个包含初始值和禁用状态的对象来重置该控件。
Resets the control with an initial value, or an object that defines the initial value and disabled state.
可选. 默认值是 null
.
options
object
当值发生变化时,该配置项会决定控件如何传播变更以及发出事件。
Configuration options that determine how the control propagates changes and emits events after the value changes.
onlySelf
:如果为 true
,则每个变更只会影响当前控件而不会影响父控件。默认为 false
。
onlySelf
: When true, each change only affects this control, and not its parent. Default is false.
emitEvent
:如果为 true
或未提供(默认),则当控件被重置时, statusChanges
和 valueChanges
这两个 Observable 都会以最近的状态和值发出事件。 如果为 false
,则不会发出事件。
emitEvent
: When true or not supplied (the default), both the statusChanges
and valueChanges
observables emit events with the latest status and value when the control is reset. When false, no events are emitted.
可选. 默认值是 {}
.
返回值 void
注册变更事件的监听器。
Register a listener for change events.
registerOnChange (fn: Function): void
registerOnChange ( fn : Function ): void
参数 fn
Function
当值变化时,就会调用该方法。
The method that is called when the value changes
返回值 void
registerOnDisabledChange() 注册禁用事件的监听器。
Register a listener for disabled events.
registerOnDisabledChange (fn: (isDisabled: boolean) => void): void
registerOnDisabledChange ( fn : ( isDisabled : boolean ) => void ): void
参数 fn
(isDisabled: boolean) => void
当禁用状态发生变化时,就会调用该方法。
The method that is called when the disabled status changes.
返回值 void
使用说明 用一个初始值初始化 FormControl
。
Instantiate a FormControl
, with an initial value.
const control = new
FormControl ('some value'); console.log(control.value); // 'some value'
content_copy
const control = new FormControl ( 'some value' );
console . log ( control . value ); // 'some value'
下面的例子用一个表单状态对象初始化控件。这里用到的是 value
和 disabled
键。
The following example initializes the control with a form state object. The value
and disabled
keys are required in this case.
const control = new
FormControl ({ value: 'n/a', disabled: true }); console.log(control.value); // 'n/a' console.log(control.status); // 'DISABLED'
content_copy
const control = new FormControl ({ value : 'n/a' , disabled : true });
console . log ( control . value ); // 'n/a'
console . log ( control . status ); // 'DISABLED'
下面的例子使用一个同步验证器初始化了该控件。
The following example initializes the control with a sync validator.
const control = new
FormControl ('', Validators.required); console.log(control.value); // '' console.log(control.status); // 'INVALID'
content_copy
const control = new FormControl ( '' , Validators . required );
console . log ( control . value ); // ''
console . log ( control . status ); // 'INVALID'
下面的例子使用一个配置对象初始化了该控件。
The following example initializes the control using an options object.
content_copy
const control = new FormControl ( '' , {
validators : Validators . required ,
asyncValidators : myAsyncValidator
});
把 updateOn
选项设置为 'blur'
,可以在发生 blur
事件时更新。
Set the updateOn
option to 'blur'
to update on the blur event
.
const control = new
FormControl ('', { updateOn: 'blur' });
content_copy
const control = new FormControl ( '' , { updateOn : 'blur' });
把 updateOn
选项设置为 'submit'
,可以在发生 submit
事件时更新。
Set the updateOn
option to 'submit'
to update on a submit event
.
const control = new
FormControl ('', { updateOn: 'submit' });
content_copy
const control = new FormControl ( '' , { updateOn : 'submit' });
把该控件重置回初始值 Reset the control back to an initial value 通过传递包含值和禁用状态的独立值或表单状态对象,可以将其重置为特定的表单状态(这是所支持的仅有的两个非计算状态)。
You reset to a specific form state by passing through a standalone value or a form state object that contains both a value and a disabled state (these are the only two properties that cannot be calculated).
const control = new
FormControl ('Nancy'); console.log(control.value); // 'Nancy' control.reset('Drew'); console.log(control.value); // 'Drew'
content_copy
const control = new FormControl ( 'Nancy' );
console . log ( control . value ); // 'Nancy'
control . reset ( 'Drew' );
console . log ( control . value ); // 'Drew'
把该控件重置回初始值并禁用。 Reset the control back to an initial value and disabled const control = new
FormControl ('Nancy'); console.log(control.value); // 'Nancy' console.log(control.status); // 'VALID' control.reset({ value: 'Drew', disabled: true }); console.log(control.value); // 'Drew' console.log(control.status); // 'DISABLED'
content_copy
const control = new FormControl ( 'Nancy' );
console . log ( control . value ); // 'Nancy'
console . log ( control . status ); // 'VALID'
control . reset ({ value : 'Drew' , disabled : true });
console . log ( control . value ); // 'Drew'
console . log ( control . status ); // 'DISABLED'