FormControl
跟踪独立表单控件的值和验证状态。
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(validators: ValidatorFn | ValidatorFn[], asyncValidators: AsyncValidatorFn | AsyncValidatorFn[])
  value: any
  validator: ValidatorFn | null
  asyncValidator: AsyncValidatorFn | null
  parent: FormGroup | FormArray | null
  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. See usage examples below.
Further information available in the Usage Notes...
构造函数
| 创建新的  Creates a new  | |||||||||
|       
      参数
 | 
方法
| 设置该表单控件的新值。 Sets a new value for the form control. | ||||||
|       
      参数
 返回值
 | 
| 修补控件的值。 Patches the value of a control. See also: 
 | 
| 在  This function is functionally the same as setValue at this level. It exists for symmetry with patchValue on  | 
| 注册变更事件的监听器。 Register a listener for change events. | 
| 注册禁用事件的监听器。 Register a listener for disabled events. | 
使用说明
初始化表单控件
Initializing Form Controls
用一个初始值初始化 FormControl。
Instantiate a FormControl, with an initial value.
      
      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'
    下面的例子使用一个同步验证器初始化了该控件。
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'
    下面的例子使用一个配置对象初始化了该控件。
The following example initializes the control using an options object.
      
      const control = new FormControl('', {
   validators: Validators.required,
   asyncValidators: myAsyncValidator
});
    配置该控件,使其在发生 blur 事件时更新
Configure the control to update on a blur event
把 updateOn 选项设置为 'blur',可以在发生 blur 事件时更新。
Set the updateOn option to 'blur' to update on the blur event.
      
      const control = new FormControl('', { updateOn: 'blur' });
    配置该控件,使其在发生 submit 事件时更新
Configure the control to update on a submit event
把 updateOn 选项设置为 'submit',可以在发生 submit 事件时更新。
Set the updateOn option to 'submit' to update on a submit event.
      
      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'
    把该控件重置回初始值并禁用。
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'
    