FormControlName
根据名字将现有 FormGroup
中的 FormControl
与一个表单控件进行同步。
Syncs a FormControl
in an existing FormGroup
to a form control element by name.
参见
NgModule
选择器
属性
属性 | 说明 |
---|---|
control: FormControl | 只读 Tracks the |
@Input('formControlName') | Tracks the name of the |
@Input('disabled') | Write-only. Triggers a warning that this input should not be used with reactive forms. |
@Input('ngModel') | |
@Output('ngModelChange') | |
path: string[] | 只读 Returns an array that represents the path from the top-level form to this control. Each index is the string name of the control on that level. |
formDirective: any | 只读 The top-level directive for this group if present, otherwise null. |
validator: ValidatorFn | null | 只读 Synchronous validator function composed of all the synchronous validators registered with this directive. |
asyncValidator: AsyncValidatorFn | 只读 Async validator function composed of all the async validators registered with this directive. |
继承自 NgControl
继承自 AbstractControlDirective
-
abstract control: AbstractControl | null
-
value: any
-
valid: boolean | null
-
invalid: boolean | null
-
pending: boolean | null
-
disabled: boolean | null
-
enabled: boolean | null
-
errors: ValidationErrors | null
-
pristine: boolean | null
-
dirty: boolean | null
-
touched: boolean | null
-
status: string | null
-
untouched: boolean | null
-
statusChanges: Observable<any> | null
-
valueChanges: Observable<any> | null
-
path: string[] | null
说明
把 FormControl
注册进一个组
Register FormControl
within a group
下面的例子演示了如何把多个表单控件注册进一个表单组,并设置它们的值。
The following example shows how to register multiple form controls within a form group and set their value.
import {Component} from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
@Component({
selector: 'example-app',
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div *ngIf="first.invalid"> Name is too short. </div>
<input formControlName="first" placeholder="First name">
<input formControlName="last" placeholder="Last name">
<button type="submit">Submit</button>
</form>
<button (click)="setValue()">Set preset value</button>
`,
})
export class SimpleFormGroup {
form = new FormGroup({
first: new FormControl('Nancy', Validators.minLength(2)),
last: new FormControl('Drew'),
});
get first(): any {
return this.form.get('first');
}
onSubmit(): void {
console.log(this.form.value); // {first: 'Nancy', last: 'Drew'}
}
setValue() {
this.form.setValue({first: 'Carson', last: 'Drew'});
}
}
要查看把 formControlName
应用于不同类型的表单控件的例子,参见:
To see formControlName
examples with different form control types, see:
单选按钮:
RadioControlValueAccessor
Radio buttons:
RadioControlValueAccessor
单选下拉框:
SelectControlValueAccessor
Selects:
SelectControlValueAccessor
和 ngModel 一起使用
Use with ngModel is deprecated
Support for using the ngModel
input property and ngModelChange
event with reactive form directives has been deprecated in Angular v6 and is scheduled for removal in a future version of Angular.
For details, see Deprecated features.
方法
A lifecycle method called when the directive's inputs change. For internal use only. | |||
参数
|
Lifecycle method called before the directive's instance is destroyed. For internal use only. |
参数没有参数。 返回值
|
Sets the new value for the view model and emits an |