2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-03-01 13:28:52 -05:00
|
|
|
import {Directive, EventEmitter, Host, HostListener, Inject, Input, OnChanges, OnDestroy, Optional, Output, Self, SimpleChanges, forwardRef} from '@angular/core';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
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-07-27 13:59:40 -04:00
|
|
|
import {AbstractFormGroupDirective} from './abstract_form_group_directive';
|
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-07-27 13:59:40 -04:00
|
|
|
import {NgForm} from './ng_form';
|
|
|
|
import {NgModelGroup} from './ng_model_group';
|
2016-06-10 13:09:50 -04:00
|
|
|
import {composeAsyncValidators, composeValidators, controlPath, isPropertyUpdated, selectValueAccessor, setUpControl} from './shared';
|
2016-07-27 13:59:40 -04:00
|
|
|
import {TemplateDrivenErrors} from './template_driven_errors';
|
2017-02-20 19:26:51 -05:00
|
|
|
import {AsyncValidator, AsyncValidatorFn, Validator, ValidatorFn} from './validators';
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-07-30 22:18:14 -04:00
|
|
|
export const formControlBinding: any = {
|
|
|
|
provide: NgControl,
|
|
|
|
useExisting: forwardRef(() => NgModel)
|
|
|
|
};
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-11-11 18:24:11 -05:00
|
|
|
/**
|
|
|
|
* `ngModel` forces an additional change detection run when its inputs change:
|
|
|
|
* E.g.:
|
|
|
|
* ```
|
|
|
|
* <div>{{myModel.valid}}</div>
|
|
|
|
* <input [(ngModel)]="myValue" #myModel="ngModel">
|
|
|
|
* ```
|
|
|
|
* I.e. `ngModel` can export itself on the element and then be used in the template.
|
|
|
|
* Normally, this would result in expressions before the `input` that use the exported directive
|
|
|
|
* to have and old value as they have been
|
|
|
|
* dirty checked before. As this is a very common case for `ngModel`, we added this second change
|
|
|
|
* detection run.
|
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* - this is just one extra run no matter how many `ngModel` have been changed.
|
|
|
|
* - this is a general problem when using `exportAs` for directives!
|
|
|
|
*/
|
2016-08-02 18:53:34 -04:00
|
|
|
const resolvedPromise = Promise.resolve(null);
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
2016-09-12 14:27:29 -04:00
|
|
|
* @whatItDoes Creates a {@link FormControl} instance from a domain model and binds it
|
|
|
|
* to a form control element.
|
|
|
|
*
|
|
|
|
* The {@link FormControl} instance will track the value, user interaction, and
|
|
|
|
* validation status of the control and keep the view synced with the model. If used
|
|
|
|
* within a parent form, the directive will also register itself with the form as a child
|
|
|
|
* control.
|
|
|
|
*
|
|
|
|
* @howToUse
|
|
|
|
*
|
|
|
|
* This directive can be used by itself or as part of a larger form. All you need is the
|
|
|
|
* `ngModel` selector to activate it.
|
|
|
|
*
|
|
|
|
* It accepts a domain model as an optional {@link @Input}. If you have a one-way binding
|
|
|
|
* to `ngModel` with `[]` syntax, changing the value of the domain model in the component
|
|
|
|
* class will set the value in the view. If you have a two-way binding with `[()]` syntax
|
|
|
|
* (also known as 'banana-box syntax'), the value in the UI will always be synced back to
|
|
|
|
* the domain model in your class as well.
|
|
|
|
*
|
|
|
|
* If you wish to inspect the properties of the associated {@link FormControl} (like
|
|
|
|
* validity state), you can also export the directive into a local template variable using
|
|
|
|
* `ngModel` as the key (ex: `#myVar="ngModel"`). You can then access the control using the
|
|
|
|
* directive's `control` property, but most properties you'll need (like `valid` and `dirty`)
|
|
|
|
* will fall through to the control anyway, so you can access them directly. You can see a
|
|
|
|
* full list of properties directly available in {@link AbstractControlDirective}.
|
|
|
|
*
|
|
|
|
* The following is an example of a simple standalone control using `ngModel`:
|
|
|
|
*
|
|
|
|
* {@example forms/ts/simpleNgModel/simple_ng_model_example.ts region='Component'}
|
|
|
|
*
|
|
|
|
* When using the `ngModel` within `<form>` tags, you'll also need to supply a `name` attribute
|
|
|
|
* so that the control can be registered with the parent form under that name.
|
|
|
|
*
|
|
|
|
* It's worth noting that in the context of a parent form, you often can skip one-way or
|
|
|
|
* two-way binding because the parent form will sync the value for you. You can access
|
|
|
|
* its properties by exporting it into a local template variable using `ngForm` (ex:
|
|
|
|
* `#f="ngForm"`). Then you can pass it where it needs to go on submit.
|
|
|
|
*
|
|
|
|
* If you do need to populate initial values into your form, using a one-way binding for
|
|
|
|
* `ngModel` tends to be sufficient as long as you use the exported form's value rather
|
|
|
|
* than the domain model's value on submit.
|
|
|
|
*
|
|
|
|
* Take a look at an example of using `ngModel` within a form:
|
|
|
|
*
|
|
|
|
* {@example forms/ts/simpleForm/simple_form_example.ts region='Component'}
|
|
|
|
*
|
2016-09-19 19:25:33 -04:00
|
|
|
* To see `ngModel` examples with different form control types, see:
|
|
|
|
*
|
|
|
|
* * Radio buttons: {@link RadioControlValueAccessor}
|
|
|
|
* * Selects: {@link SelectControlValueAccessor}
|
|
|
|
*
|
2016-09-12 14:27:29 -04:00
|
|
|
* **npm package**: `@angular/forms`
|
|
|
|
*
|
|
|
|
* **NgModule**: `FormsModule`
|
2016-06-08 18:36:24 -04:00
|
|
|
*
|
2016-08-17 10:44:39 -04:00
|
|
|
* @stable
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
|
|
|
@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],
|
2016-06-15 20:46:45 -04:00
|
|
|
exportAs: 'ngModel'
|
2016-06-08 18:36:24 -04:00
|
|
|
})
|
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-23 12:55:26 -04:00
|
|
|
_control = new FormControl();
|
2016-06-08 18:36:24 -04:00
|
|
|
/** @internal */
|
2016-06-23 12:55:26 -04:00
|
|
|
_registered = false;
|
2017-01-12 07:02:45 -05:00
|
|
|
private _composing = false;
|
2016-06-08 18:36:24 -04:00
|
|
|
viewModel: any;
|
|
|
|
|
2016-06-10 13:09:50 -04:00
|
|
|
@Input() name: string;
|
2016-08-25 17:56:31 -04:00
|
|
|
@Input('disabled') isDisabled: boolean;
|
2016-08-24 19:58:43 -04:00
|
|
|
@Input('ngModel') model: any;
|
2016-06-23 12:55:26 -04:00
|
|
|
@Input('ngModelOptions') options: {name?: string, standalone?: boolean};
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2016-06-10 13:09:50 -04:00
|
|
|
@Output('ngModelChange') update = new EventEmitter();
|
|
|
|
|
2017-01-12 07:02:45 -05:00
|
|
|
@HostListener('compositionstart')
|
|
|
|
compositionStart(): void { this._composing = true; }
|
|
|
|
|
|
|
|
@HostListener('compositionend')
|
|
|
|
compositionEnd(): void {
|
|
|
|
this._composing = false;
|
|
|
|
this.update.emit(this.viewModel);
|
|
|
|
}
|
|
|
|
|
2016-08-29 20:49:42 -04:00
|
|
|
constructor(@Optional() @Host() parent: ControlContainer,
|
2016-08-29 14:33:49 -04:00
|
|
|
@Optional() @Self() @Inject(NG_VALIDATORS) validators: Array<Validator|ValidatorFn>,
|
2017-02-20 19:26:51 -05:00
|
|
|
@Optional() @Self() @Inject(NG_ASYNC_VALIDATORS) asyncValidators: Array<AsyncValidator|AsyncValidatorFn>,
|
2016-06-08 18:36:24 -04:00
|
|
|
@Optional() @Self() @Inject(NG_VALUE_ACCESSOR)
|
|
|
|
valueAccessors: ControlValueAccessor[]) {
|
2016-06-08 19:38:52 -04:00
|
|
|
super();
|
2016-08-29 20:49:42 -04:00
|
|
|
this._parent = parent;
|
2016-08-29 14:33:49 -04:00
|
|
|
this._rawValidators = validators || [];
|
|
|
|
this._rawAsyncValidators = asyncValidators || [];
|
2016-06-08 19:38:52 -04:00
|
|
|
this.valueAccessor = selectValueAccessor(this, valueAccessors);
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
2016-07-27 13:59:40 -04:00
|
|
|
this._checkForErrors();
|
2016-06-23 12:55:26 -04:00
|
|
|
if (!this._registered) this._setUpControl();
|
2016-08-25 17:56:31 -04:00
|
|
|
if ('isDisabled' in changes) {
|
2016-08-24 19:58:43 -04:00
|
|
|
this._updateDisabled(changes);
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
if (isPropertyUpdated(changes, this.viewModel)) {
|
2016-06-23 21:07:56 -04:00
|
|
|
this._updateValue(this.model);
|
2016-06-08 19:38:52 -04:00
|
|
|
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[] {
|
2016-07-13 17:13:02 -04:00
|
|
|
return this._parent ? controlPath(this.name, this._parent) : [this.name];
|
2016-06-10 13:09:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get formDirective(): any { return this._parent ? this._parent.formDirective : null; }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-08-29 14:33:49 -04:00
|
|
|
get validator(): ValidatorFn { return composeValidators(this._rawValidators); }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
get asyncValidator(): AsyncValidatorFn {
|
2016-08-29 14:33:49 -04:00
|
|
|
return composeAsyncValidators(this._rawAsyncValidators);
|
2016-06-08 19:38:52 -04:00
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
viewToModelUpdate(newValue: any): void {
|
|
|
|
this.viewModel = newValue;
|
2017-01-12 07:02:45 -05:00
|
|
|
!this._composing && this.update.emit(newValue);
|
2016-06-08 19:38:52 -04:00
|
|
|
}
|
2016-06-10 13:09:50 -04:00
|
|
|
|
2016-06-23 12:55:26 -04:00
|
|
|
private _setUpControl(): void {
|
|
|
|
this._isStandalone() ? this._setUpStandalone() :
|
|
|
|
this.formDirective.addControl(this);
|
|
|
|
this._registered = true;
|
2016-06-10 13:09:50 -04:00
|
|
|
}
|
|
|
|
|
2016-06-23 12:55:26 -04:00
|
|
|
private _isStandalone(): boolean {
|
|
|
|
return !this._parent || (this.options && this.options.standalone);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _setUpStandalone(): void {
|
2016-06-10 13:09:50 -04:00
|
|
|
setUpControl(this._control, this);
|
|
|
|
this._control.updateValueAndValidity({emitEvent: false});
|
|
|
|
}
|
|
|
|
|
2016-07-27 13:59:40 -04:00
|
|
|
private _checkForErrors(): void {
|
|
|
|
if (!this._isStandalone()) {
|
|
|
|
this._checkParentType();
|
|
|
|
}
|
|
|
|
this._checkName();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _checkParentType(): void {
|
|
|
|
if (!(this._parent instanceof NgModelGroup) &&
|
|
|
|
this._parent instanceof AbstractFormGroupDirective) {
|
|
|
|
TemplateDrivenErrors.formGroupNameException();
|
|
|
|
} else if (
|
|
|
|
!(this._parent instanceof NgModelGroup) && !(this._parent instanceof NgForm)) {
|
|
|
|
TemplateDrivenErrors.modelParentException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 21:07:56 -04:00
|
|
|
private _checkName(): void {
|
2016-06-13 19:32:45 -04:00
|
|
|
if (this.options && this.options.name) this.name = this.options.name;
|
|
|
|
|
2016-06-23 12:55:26 -04:00
|
|
|
if (!this._isStandalone() && !this.name) {
|
2016-07-27 13:59:40 -04:00
|
|
|
TemplateDrivenErrors.missingNameException();
|
2016-06-10 13:09:50 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-23 21:07:56 -04:00
|
|
|
|
|
|
|
private _updateValue(value: any): void {
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then(
|
2016-08-05 16:35:17 -04:00
|
|
|
() => { this.control.setValue(value, {emitViewToModelChange: false}); });
|
2016-06-23 21:07:56 -04:00
|
|
|
}
|
2016-08-24 19:58:43 -04:00
|
|
|
|
|
|
|
private _updateDisabled(changes: SimpleChanges) {
|
2016-08-25 17:56:31 -04:00
|
|
|
const disabledValue = changes['isDisabled'].currentValue;
|
2016-09-20 17:55:47 -04:00
|
|
|
|
|
|
|
const isDisabled =
|
|
|
|
disabledValue === '' || (disabledValue && disabledValue !== 'false');
|
2016-08-24 19:58:43 -04:00
|
|
|
|
|
|
|
resolvedPromise.then(() => {
|
|
|
|
if (isDisabled && !this.control.disabled) {
|
|
|
|
this.control.disable();
|
|
|
|
} else if (!isDisabled && this.control.disabled) {
|
|
|
|
this.control.enable();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|