Creates and binds a control with a specified name to a DOM element.
This directive can only be used as a child of NgForm
or NgFormModel
.
Example
In this example, we create the login and password controls. We can work with each control separately: check its validity, get its value, listen to its changes.
@Component(
selector: "login-comp",
directives: const [FORM_DIRECTIVES],
template: '''
<form #f="ngForm" (submit)='onLogIn(f.value)'>
Login <input type='text' ngControl='login' #l="form">
<div *ngIf="!l.valid">Login is invalid</div>
Password <input type='password' ngControl='password'>
<button type='submit'>Log in!</button>
</form>
''')
class LoginComp {
void onLogIn(value) {
// value === {'login': 'some login', 'password': 'some password'}
}
}
We can also use ngModel to bind a domain model to the form.
@Component(
selector: "login-comp",
directives: [FORM_DIRECTIVES],
template: '''
<form (submit)='onLogIn()'>
Login <input type='text' ngControl='login' [(ngModel)]="credentials.login">
Password <input type='password' ngControl='password'
[(ngModel)]="credentials.password">
<button type='submit'>Log in!</button>
</form>
''')
class LoginComp {
credentials: {login:string, password:string};
onLogIn(): void {
// this.credentials.login === "some login"
// this.credentials.password === "some password"
}
}
- Inheritance
- Object
- AbstractControlDirective
- NgControl
- NgControlName
- Implements
-
- OnChanges
- OnDestroy
- Annotations
- Directive(selector: "[ngControl]", providers: const [controlNameBinding], inputs: const ["name: ngControl", "model: ngModel"], outputs: const ["update: ngModelChange"], exportAs: "ngForm")
Constructors
- NgControlName(@ ControlContainer _parent, @ @ @ List _validators, @ @ @ List _asyncValidators, @ @ @ List<ControlValueAccessor> valueAccessors)
Properties
- asyncValidator → AsyncValidatorFn
-
read-only
- control → Control
-
read-only
- dirty → bool
-
read-only, inherited
- errors → Map<String, dynamic>
-
read-only, inherited
- formDirective → dynamic
-
read-only
- hashCode → int
-
Get a hash code for this object.
read-only, inherited - model → dynamic
-
read / write
- name → String
-
read / write, inherited
- path → List<String>
-
read-only
- pristine → bool
-
read-only, inherited
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited - touched → bool
-
read-only, inherited
- untouched → bool
-
read-only, inherited
- update → dynamic
-
read / write
- valid → bool
-
read-only, inherited
- validator → ValidatorFn
-
read-only
- value → dynamic
-
read-only, inherited
- valueAccessor → ControlValueAccessor
-
read / write, inherited
- viewModel → dynamic
-
read / write
Operators
-
operator ==(
other) → bool -
The equality operator.
inherited
Methods
-
ngOnChanges(
Map<String, SimpleChange> changes) → dynamic -
ngOnDestroy(
) → void -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed.
inherited -
toString(
) → String -
Returns a string representation of this object.
inherited -
viewToModelUpdate(
newValue) → void