2015-07-27 22:12:30 -07:00
p.location-badge.
exported from <a href='../forms'>angular2/forms</a>
2015-08-06 23:25:42 -07:00
defined in <a href="https://github.com/angular/angular/tree/2.0.0-alpha.34/modules/angular2/src/forms/directives/ng_control_name.ts#L16-L123">angular2/src/forms/directives/ng_control_name.ts (line 16)</a>
2015-07-27 22:12:30 -07:00
:markdown
Creates and binds a control with a specified name to a DOM element.
This directive can only be used as a child of <a href='NgForm-class.html'><code>NgForm</code></a> or <a href='NgFormModel-class.html'><code>NgFormModel</code></a>.
# 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"})
@View({
directives: [formDirectives],
template: `
<form #f="form" (submit)='onLogIn(f.value)'>
Login <input type='text' ng-control='login' #l="form">
<div *ng-if="!l.valid">Login is invalid</div>
Password <input type='password' ng-control='password'>
<button type='submit'>Log in!</button>
</form>
`})
class LoginComp {
onLogIn(value) {
// value === {login: 'some login', password: 'some password'}
}
}
```
We can also use ng-model to bind a domain model to the form.
```
@Component({selector: "login-comp"})
@View({
directives: [formDirectives],
template: `
<form (submit)='onLogIn()'>
Login <input type='text' ng-control='login' [(ng-model)]="credentials.login">
Password <input type='password' ng-control='password'
[(ng-model)]="credentials.password">
<button type='submit'>Log in!</button>
</form>
`})
class LoginComp {
credentials: {login:string, password:string};
onLogIn() {
// this.credentials.login === "some login"
// this.credentials.password === "some password"
}
}
```
2015-07-28 13:28:47 -07:00
.l-main-section
h2 Annotations
.l-sub-section
h3.annotation Directive
pre.prettyprint
code.
@Directive({
selector: '[ng-control]',
2015-08-02 23:08:36 -07:00
bindings: [controlNameBinding],
2015-07-28 13:28:47 -07:00
properties: ['name: ngControl', 'model: ngModel'],
events: ['update: ngModel'],
lifecycle: [LifecycleEvent.onDestroy, LifecycleEvent.onChange],
exportAs: 'form'
})
2015-07-27 22:12:30 -07:00
.l-main-section
h2 Members
.l-sub-section
h3 constructor
pre.prettyprint
code.
constructor(parent: ControlContainer, ngValidators: QueryList<NgValidator>)
:markdown
.l-sub-section
h3 update
:markdown
.l-sub-section
h3 model
:markdown
.l-sub-section
h3 viewModel
:markdown
.l-sub-section
h3 ngValidators
:markdown
.l-sub-section
h3 onChange
pre.prettyprint
code.
onChange(c: StringMap<string, any>)
:markdown
.l-sub-section
h3 onDestroy
pre.prettyprint
code.
onDestroy()
:markdown
.l-sub-section
h3 viewToModelUpdate
pre.prettyprint
code.
viewToModelUpdate(newValue: any)
:markdown
.l-sub-section
h3 path
:markdown
.l-sub-section
h3 formDirective
:markdown
.l-sub-section
h3 control
:markdown
.l-sub-section
h3 validator
:markdown