FormGroup
跟踪一组 FormControl
实例的值和有效性状态。
Tracks the value and validity state of a group of FormControl
instances.
class FormGroup extends AbstractControl {
constructor(controls: { [key: string]: AbstractControl; }, validatorOrOpts?: ValidatorFn | AbstractControlOptions | ValidatorFn[], asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[])
controls: {...}
registerControl(name: string, control: AbstractControl): AbstractControl
addControl(name: string, control: AbstractControl, options: { emitEvent?: boolean; } = {}): void
removeControl(name: string, options: { emitEvent?: boolean; } = {}): void
setControl(name: string, control: AbstractControl, options: { emitEvent?: boolean; } = {}): void
contains(controlName: string): boolean
setValue(value: { [key: string]: any; }, options: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
patchValue(value: { [key: string]: any; }, options: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
reset(value: any = {}, options: { onlySelf?: boolean; emitEvent?: boolean; } = {}): void
getRawValue(): any
// 继承自 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
把每个子 FormControl
的值聚合进一个对象,它的 key 是每个控件的名字。 它通过归集其子控件的状态值来计算出自己的状态。 比如,如果组中的任何一个控件是无效的,那么整个组就是无效的。
A FormGroup
aggregates the values of each child FormControl
into one object, with each control name as the key. It calculates its status by reducing the status values of its children. For example, if one of the controls in a group is invalid, the entire group becomes invalid.
FormGroup
是 Angular 中用来定义表单的三大基本构造块之一,就像 FormControl
、FormArray
一样。
FormGroup
is one of the three fundamental building blocks used to define forms in Angular, along with FormControl
and FormArray
.
当实例化 FormGroup
时,在第一个参数中传入一组子控件。每个子控件会用控件名把自己注册进去。
When instantiating a FormGroup
, pass in a collection of child controls as the first argument. The key for each child registers the name for the control.
Further information available in the Usage Notes...
构造函数
创建一个新的 Creates a new | |||||||||
参数
|
属性
属性 | 说明 |
---|---|
controls: { [key: string]: AbstractControl; } | 声明在构造函数中 一组子控件。每个子控件的名字就是它注册时用的 A collection of child controls. The key for each child is the name under which it is registered. |
方法
向组内的控件列表中注册一个控件。 Registers a control with the group's list of controls. | ||||||
参数
返回值 | ||||||
该方法不会更新控件的值或其有效性。 使用 addControl 代替。 This method does not update the value or validity of the control. Use addControl instead. |
往组中添加一个控件。 Add a control to this group. | |||||||||
参数
返回值
| |||||||||
该方法还会更新本空间的值和有效性。 This method also updates the value and validity of the control. |
替换现有控件。 Replace an existing control. | |||||||||
参数
返回值
|
检查组内是否有一个具有指定名字的已启用的控件。 Check whether there is an enabled control with the given name in the group. |
对于已禁用的控件,返回 Reports false for disabled controls. If you'd like to check for existence in the group only, use get instead. |
设置此 Sets the value of the | ||||||
参数
返回值
异常
当严格的检查失败时,比如设置了不存在的或被排除出去的控件的值。 | ||||||
使用说明设置表单组的完整值Set the complete value for the form group
|
修补此 Patches the value of the | ||||||
参数
返回值
| ||||||
它能接受组的超集和子集,而不会抛出错误。 It accepts both super-sets and sub-sets of the group without throwing an error. | ||||||
使用说明修补表单组的值Patch the value for a form group
|
重置这个 Resets the | ||||||
参数
返回值
| ||||||
你可以通过传入一个与表单结构相匹配的以控件名为 key 的 Map,来把表单重置为特定的状态。 其状态可以是一个单独的值,也可以是一个同时具有值和禁用状态的表单状态对象。 You reset to a specific form state by passing in a map of states that matches the structure of your form, with control names as keys. The state is a standalone value or a form state object with both a value and a disabled status. | ||||||
使用说明重置该表单组的值Reset the form group values
重置该表单组的值以及禁用状态Reset the form group values and disabled status
|
这个 The aggregate value of the |
参数没有参数。 返回值
|
获取所有控件的值而不管其禁用状态。 Retrieves all values regardless of disabled status. The |
使用说明
创建一个带有两个控件的表单组
Create a form group with 2 controls
const form = new FormGroup({
first: new FormControl('Nancy', Validators.minLength(2)),
last: new FormControl('Drew'),
});
console.log(form.value); // {first: 'Nancy', last; 'Drew'}
console.log(form.status); // 'VALID'
创建一个具有组级验证器的表单组
Create a form group with a group-level validator
你可以用第二个参数传入一些组级验证器或用第三个参数传入一些组级异步验证器。 当你要根据一个以上子控件的值来决定有效性时,这很好用。
You include group-level validators as the second arg, or group-level async validators as the third arg. These come in handy when you want to perform validation that considers the value of more than one child control.
const form = new FormGroup({
password: new FormControl('', Validators.minLength(2)),
passwordConfirm: new FormControl('', Validators.minLength(2)),
}, passwordMatchValidator);
function passwordMatchValidator(g: FormGroup) {
return g.get('password').value === g.get('passwordConfirm').value
? null : {'mismatch': true};
}
像 FormControl
实例一样,你也可以在配置对象中传入验证器和异步验证器。
Like FormControl
instances, you choose to pass in validators and async validators as part of an options object.
const form = new FormGroup({
password: new FormControl('')
passwordConfirm: new FormControl('')
}, { validators: passwordMatchValidator, asyncValidators: otherValidator });
为表单组中的所有空间设置 updateOn
属性
Set the updateOn property for all controls in a form group
该选项对象可用来为每个子控件的 updateOn
属性设置默认值。 如果在组级把 updateOn
设置为 'blur'
,则所有子控件的默认值也是 'blur'
,除非这个子控件显式的指定了另一个 updateOn
值。
The options object is used to set a default value for each child control's updateOn
property. If you set updateOn
to 'blur'
at the group level, all child controls default to 'blur', unless the child has explicitly specified a different updateOn
value.
const c = new FormGroup({
one: new FormControl()
}, { updateOn: 'blur' });