2015-05-30 11:56:00 -07:00
|
|
|
import {Directive, Ancestor, onDestroy, onInit} from 'angular2/angular2';
|
|
|
|
import {Inject, FORWARD_REF, Binding} from 'angular2/di';
|
|
|
|
import {List, ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {CONST_EXPR} from 'angular2/src/facade/lang';
|
|
|
|
|
|
|
|
import {ControlContainerDirective} from './control_container_directive';
|
|
|
|
import {controlPath} from './shared';
|
|
|
|
|
|
|
|
const controlGroupBinding = CONST_EXPR(
|
|
|
|
new Binding(ControlContainerDirective, {toAlias: FORWARD_REF(() => ControlGroupDirective)}));
|
|
|
|
|
|
|
|
/**
|
2015-06-02 14:53:49 -07:00
|
|
|
* Binds a ng-control group to a DOM element.
|
2015-05-30 11:56:00 -07:00
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
2015-06-02 14:53:49 -07:00
|
|
|
* In this example, we create a ng-control group, and we bind the login and
|
2015-05-31 12:24:34 -07:00
|
|
|
* password controls to the login and password elements.
|
2015-05-30 11:56:00 -07:00
|
|
|
*
|
|
|
|
* Here we use {@link formDirectives}, rather than importing each form directive individually, e.g.
|
|
|
|
* `ControlDirective`, `ControlGroupDirective`. This is just a shorthand for the same end result.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component({selector: "login-comp"})
|
|
|
|
* @View({
|
|
|
|
* directives: [formDirectives],
|
2015-05-31 12:24:34 -07:00
|
|
|
* template:
|
2015-06-02 14:53:49 -07:00
|
|
|
* "<form [ng-form-model]='loginForm'>" +
|
|
|
|
* "<div ng-control-group="credentials">
|
|
|
|
* "Login <input type='text' ng-control='login'>" +
|
|
|
|
* "Password <input type='password' ng-control='password'>" +
|
2015-05-30 11:56:00 -07:00
|
|
|
* "<button (click)="onLogin()">Login</button>" +
|
2015-05-31 12:24:34 -07:00
|
|
|
* "</div>"
|
2015-05-30 11:56:00 -07:00
|
|
|
* "</form>"
|
|
|
|
* })
|
|
|
|
* class LoginComp {
|
|
|
|
* loginForm:ControlGroup;
|
|
|
|
*
|
|
|
|
* constructor() {
|
|
|
|
* this.loginForm = new ControlGroup({
|
2015-05-31 12:24:34 -07:00
|
|
|
* credentials: new ControlGroup({
|
2015-06-02 14:53:49 -07:00
|
|
|
* login: new Cntrol(""),
|
2015-05-31 12:24:34 -07:00
|
|
|
* password: new Control("")
|
|
|
|
* })
|
2015-05-30 11:56:00 -07:00
|
|
|
* });
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* onLogin() {
|
|
|
|
* // this.loginForm.value
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @exportedAs angular2/forms
|
|
|
|
*/
|
|
|
|
@Directive({
|
2015-06-02 14:53:49 -07:00
|
|
|
selector: '[ng-control-group]',
|
2015-05-30 11:56:00 -07:00
|
|
|
hostInjector: [controlGroupBinding],
|
2015-06-02 14:53:49 -07:00
|
|
|
properties: ['name: ng-control-group'],
|
2015-06-05 14:33:57 -07:00
|
|
|
lifecycle: [onInit, onDestroy],
|
|
|
|
exportAs: 'form'
|
2015-05-30 11:56:00 -07:00
|
|
|
})
|
|
|
|
export class ControlGroupDirective extends ControlContainerDirective {
|
|
|
|
_parent: ControlContainerDirective;
|
|
|
|
constructor(@Ancestor() _parent: ControlContainerDirective) {
|
|
|
|
super();
|
|
|
|
this._parent = _parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
onInit() { this.formDirective.addControlGroup(this); }
|
|
|
|
|
|
|
|
onDestroy() { this.formDirective.removeControlGroup(this); }
|
|
|
|
|
|
|
|
get path(): List<string> { return controlPath(this.name, this._parent); }
|
|
|
|
|
|
|
|
get formDirective(): any { return this._parent.formDirective; }
|
|
|
|
}
|