UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

130 lines (128 loc) 5.42 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; import { CONST_EXPR } from 'angular2/src/facade/lang'; import { EventEmitter, ObservableWrapper } from 'angular2/src/facade/async'; import { Directive, forwardRef, Host, SkipSelf, Provider, Inject, Optional, Self } from 'angular2/core'; import { ControlContainer } from './control_container'; import { NgControl } from './ng_control'; import { NG_VALUE_ACCESSOR } from './control_value_accessor'; import { controlPath, composeValidators, composeAsyncValidators, isPropertyUpdated, selectValueAccessor } from './shared'; import { NG_VALIDATORS, NG_ASYNC_VALIDATORS } from '../validators'; const controlNameBinding = CONST_EXPR(new Provider(NgControl, { useExisting: forwardRef(() => NgControlName) })); /** * Creates and binds a control with a specified name to a DOM element. * * This directive can only be used as a child of {@link NgForm} or {@link 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: [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 { * onLogIn(value): void { * // 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" * } * } * ``` */ export let NgControlName = class NgControlName extends NgControl { constructor(_parent, _validators, _asyncValidators, valueAccessors) { super(); this._parent = _parent; this._validators = _validators; this._asyncValidators = _asyncValidators; /** @internal */ this.update = new EventEmitter(); this._added = false; this.valueAccessor = selectValueAccessor(this, valueAccessors); } ngOnChanges(changes) { if (!this._added) { this.formDirective.addControl(this); this._added = true; } if (isPropertyUpdated(changes, this.viewModel)) { this.viewModel = this.model; this.formDirective.updateModel(this, this.model); } } ngOnDestroy() { this.formDirective.removeControl(this); } viewToModelUpdate(newValue) { this.viewModel = newValue; ObservableWrapper.callEmit(this.update, newValue); } get path() { return controlPath(this.name, this._parent); } get formDirective() { return this._parent.formDirective; } get validator() { return composeValidators(this._validators); } get asyncValidator() { return composeAsyncValidators(this._asyncValidators); } get control() { return this.formDirective.getControl(this); } }; NgControlName = __decorate([ Directive({ selector: '[ngControl]', bindings: [controlNameBinding], inputs: ['name: ngControl', 'model: ngModel'], outputs: ['update: ngModelChange'], exportAs: 'ngForm' }), __param(0, Host()), __param(0, SkipSelf()), __param(1, Optional()), __param(1, Self()), __param(1, Inject(NG_VALIDATORS)), __param(2, Optional()), __param(2, Self()), __param(2, Inject(NG_ASYNC_VALIDATORS)), __param(3, Optional()), __param(3, Self()), __param(3, Inject(NG_VALUE_ACCESSOR)), __metadata('design:paramtypes', [ControlContainer, Array, Array, Array]) ], NgControlName);