UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

118 lines 4.91 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc); switch (arguments.length) { case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target); case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0); case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc); } }; 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 { StringMapWrapper } from 'angular2/src/facade/collection'; import { EventEmitter, ObservableWrapper } from 'angular2/src/facade/async'; import { Directive } from 'angular2/src/core/metadata'; import { forwardRef, Provider, Inject, Optional } from 'angular2/src/core/di'; import { NgControl } from './ng_control'; import { NG_VALIDATORS, NG_ASYNC_VALIDATORS } from '../validators'; import { NG_VALUE_ACCESSOR } from './control_value_accessor'; import { setUpControl, composeValidators, composeAsyncValidators, isPropertyUpdated, selectValueAccessor } from './shared'; const formControlBinding = CONST_EXPR(new Provider(NgControl, { useExisting: forwardRef(() => NgFormControl) })); /** * Binds an existing {@link Control} to a DOM element. * * ### Example ([live demo](http://plnkr.co/edit/jcQlZ2tTh22BZZ2ucNAT?p=preview)) * * In this example, we bind the control to an input element. When the value of the input element * changes, the value of the control will reflect that change. Likewise, if the value of the * control changes, the input element reflects that change. * * ```typescript * @Component({ * selector: 'my-app', * template: ` * <div> * <h2>NgFormControl Example</h2> * <form> * <p>Element with existing control: <input type="text" * [ng-form-control]="loginControl"></p> * <p>Value of existing control: {{loginControl.value}}</p> * </form> * </div> * `, * directives: [CORE_DIRECTIVES, FORM_DIRECTIVES] * }) * export class App { * loginControl: Control = new Control(''); * } * ``` * *##ng-model * * We can also use `ng-model` to bind a domain model to the form. * * ### Example ([live demo](http://plnkr.co/edit/yHMLuHO7DNgT8XvtjTDH?p=preview)) * * ```typescript * @Component({ * selector: "login-comp", * directives: [FORM_DIRECTIVES], * template: "<input type='text' [ng-form-control]='loginControl' [(ng-model)]='login'>" * }) * class LoginComp { * loginControl: Control = new Control(''); * login:string; * } * ``` */ export let NgFormControl = class extends NgControl { constructor(_validators, _asyncValidators, valueAccessors) { super(); this._validators = _validators; this._asyncValidators = _asyncValidators; this.update = new EventEmitter(); this.valueAccessor = selectValueAccessor(this, valueAccessors); } onChanges(changes) { if (this._isControlChanged(changes)) { setUpControl(this.form, this); this.form.updateValueAndValidity({ emitEvent: false }); } if (isPropertyUpdated(changes, this.viewModel)) { this.form.updateValue(this.model); this.viewModel = this.model; } } get path() { return []; } get validator() { return composeValidators(this._validators); } get asyncValidator() { return composeAsyncValidators(this._asyncValidators); } get control() { return this.form; } viewToModelUpdate(newValue) { this.viewModel = newValue; ObservableWrapper.callNext(this.update, newValue); } _isControlChanged(changes) { return StringMapWrapper.contains(changes, "form"); } }; NgFormControl = __decorate([ Directive({ selector: '[ng-form-control]', bindings: [formControlBinding], inputs: ['form: ngFormControl', 'model: ngModel'], outputs: ['update: ngModelChange'], exportAs: 'form' }), __param(0, Optional()), __param(0, Inject(NG_VALIDATORS)), __param(1, Optional()), __param(1, Inject(NG_ASYNC_VALIDATORS)), __param(2, Optional()), __param(2, Inject(NG_VALUE_ACCESSOR)), __metadata('design:paramtypes', [Array, Array, Array]) ], NgFormControl); //# sourceMappingURL=ng_form_control.js.map