2016-06-10 13:09:50 -04:00
|
|
|
import {Directive, Host, Inject, Input, OnChanges, OnDestroy, Optional, Output, Self, SimpleChanges, forwardRef} from '@angular/core';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-06-14 21:23:40 -04:00
|
|
|
import {EventEmitter, ObservableWrapper} from '../facade/async';
|
|
|
|
import {BaseException} from '../facade/exceptions';
|
2016-06-10 14:15:59 -04:00
|
|
|
import {FormControl} from '../model';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../validators';
|
|
|
|
|
2016-06-10 13:09:50 -04:00
|
|
|
import {ControlContainer} from './control_container';
|
2016-06-08 18:36:24 -04:00
|
|
|
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
|
|
|
|
import {NgControl} from './ng_control';
|
2016-06-10 13:09:50 -04:00
|
|
|
import {composeAsyncValidators, composeValidators, controlPath, isPropertyUpdated, selectValueAccessor, setUpControl} from './shared';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {AsyncValidatorFn, ValidatorFn} from './validators';
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
export const formControlBinding: any =
|
|
|
|
/*@ts2dart_const*/ /* @ts2dart_Provider */ {
|
|
|
|
provide: NgControl,
|
|
|
|
useExisting: forwardRef(() => NgModel)
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds a domain model to a form control.
|
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* `ngModel` binds an existing domain model to a form control. For a
|
|
|
|
* two-way binding, use `[(ngModel)]` to ensure the model updates in
|
|
|
|
* both directions.
|
|
|
|
*
|
|
|
|
* ```typescript
|
|
|
|
* @Component({
|
|
|
|
* selector: "search-comp",
|
2016-06-13 19:32:45 -04:00
|
|
|
* directives: [],
|
2016-06-08 18:36:24 -04:00
|
|
|
* template: `<input type='text' [(ngModel)]="searchQuery">`
|
|
|
|
* })
|
|
|
|
* class SearchComp {
|
|
|
|
* searchQuery: string;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
@Directive({
|
2016-06-12 16:17:07 -04:00
|
|
|
selector: '[ngModel]:not([formControlName]):not([formControl])',
|
2016-06-08 18:36:24 -04:00
|
|
|
providers: [formControlBinding],
|
|
|
|
exportAs: 'ngForm'
|
|
|
|
})
|
2016-06-10 13:09:50 -04:00
|
|
|
export class NgModel extends NgControl implements OnChanges,
|
|
|
|
OnDestroy {
|
2016-06-08 18:36:24 -04:00
|
|
|
/** @internal */
|
2016-06-10 14:15:59 -04:00
|
|
|
_control: FormControl;
|
2016-06-08 18:36:24 -04:00
|
|
|
/** @internal */
|
|
|
|
_added = false;
|
|
|
|
viewModel: any;
|
|
|
|
|
2016-06-10 13:09:50 -04:00
|
|
|
@Input('ngModel') model: any;
|
|
|
|
@Input() name: string;
|
2016-06-13 19:32:45 -04:00
|
|
|
@Input('ngModelOptions') options: {name?: string};
|
2016-06-10 13:09:50 -04:00
|
|
|
@Output('ngModelChange') update = new EventEmitter();
|
|
|
|
|
|
|
|
constructor(@Optional() @Host() private _parent: ControlContainer,
|
|
|
|
@Optional() @Self() @Inject(NG_VALIDATORS) private _validators: any[],
|
2016-06-08 18:36:24 -04:00
|
|
|
@Optional() @Self() @Inject(NG_ASYNC_VALIDATORS) private _asyncValidators: any[],
|
|
|
|
@Optional() @Self() @Inject(NG_VALUE_ACCESSOR)
|
|
|
|
valueAccessors: ControlValueAccessor[]) {
|
2016-06-08 19:38:52 -04:00
|
|
|
super();
|
|
|
|
this.valueAccessor = selectValueAccessor(this, valueAccessors);
|
2016-06-10 14:15:59 -04:00
|
|
|
if (!this._parent) this._control = new FormControl();
|
2016-06-08 19:38:52 -04:00
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
2016-06-10 13:09:50 -04:00
|
|
|
this._checkName();
|
|
|
|
if (!this._added) this._addControl();
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
if (isPropertyUpdated(changes, this.viewModel)) {
|
|
|
|
this._control.updateValue(this.model);
|
|
|
|
this.viewModel = this.model;
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-10 13:09:50 -04:00
|
|
|
ngOnDestroy(): void { this.formDirective && this.formDirective.removeControl(this); }
|
|
|
|
|
2016-06-10 14:15:59 -04:00
|
|
|
get control(): FormControl { return this._control; }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-10 13:09:50 -04:00
|
|
|
get path(): string[] {
|
|
|
|
return this._parent ? controlPath(this.name, this._parent) : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
get formDirective(): any { return this._parent ? this._parent.formDirective : null; }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
get validator(): ValidatorFn { return composeValidators(this._validators); }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
get asyncValidator(): AsyncValidatorFn {
|
|
|
|
return composeAsyncValidators(this._asyncValidators);
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
viewToModelUpdate(newValue: any): void {
|
|
|
|
this.viewModel = newValue;
|
|
|
|
ObservableWrapper.callEmit(this.update, newValue);
|
|
|
|
}
|
2016-06-10 13:09:50 -04:00
|
|
|
|
|
|
|
private _addControl(): void {
|
|
|
|
this._control = this.formDirective ? this.formDirective.addControl(this) :
|
|
|
|
this._addStandaloneControl();
|
|
|
|
this._added = true;
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:15:59 -04:00
|
|
|
private _addStandaloneControl(): FormControl {
|
2016-06-10 13:09:50 -04:00
|
|
|
setUpControl(this._control, this);
|
|
|
|
this._control.updateValueAndValidity({emitEvent: false});
|
|
|
|
return this._control;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _checkName() {
|
2016-06-13 19:32:45 -04:00
|
|
|
if (this.options && this.options.name) this.name = this.options.name;
|
|
|
|
|
2016-06-10 13:09:50 -04:00
|
|
|
if (this._parent && !this.name) {
|
|
|
|
throw new BaseException(
|
|
|
|
`Name attribute must be set if ngModel is used within a form.
|
|
|
|
Example: <input [(ngModel)]="person.firstName" name="first">`);
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|