|
8 | 8 |
|
9 | 9 | import {Directive, EventEmitter, Host, Inject, Input, OnChanges, OnDestroy, Optional, Output, Self, SimpleChanges, forwardRef} from '@angular/core'; |
10 | 10 |
|
11 | | -import {FormControl} from '../model'; |
| 11 | +import {FormControl, FormHooks} from '../model'; |
12 | 12 | import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../validators'; |
13 | 13 |
|
14 | 14 | import {AbstractFormGroupDirective} from './abstract_form_group_directive'; |
@@ -119,7 +119,45 @@ export class NgModel extends NgControl implements OnChanges, |
119 | 119 | @Input() name: string; |
120 | 120 | @Input('disabled') isDisabled: boolean; |
121 | 121 | @Input('ngModel') model: any; |
122 | | - @Input('ngModelOptions') options: {name?: string, standalone?: boolean}; |
| 122 | + |
| 123 | + /** |
| 124 | + * Options object for this `ngModel` instance. You can configure the following properties: |
| 125 | + * |
| 126 | + * **name**: An alternative to setting the name attribute on the form control element. |
| 127 | + * Sometimes, especially with custom form components, the name attribute might be used |
| 128 | + * as an `@Input` property for a different purpose. In cases like these, you can configure |
| 129 | + * the `ngModel` name through this option. |
| 130 | + * |
| 131 | + * ```html |
| 132 | + * <form> |
| 133 | + * <my-person-control name="Nancy" ngModel [ngModelOptions]="{name: 'user'}"> |
| 134 | + * </my-person-control> |
| 135 | + * </form> |
| 136 | + * <!-- form value: {user: ''} --> |
| 137 | + * ``` |
| 138 | + * |
| 139 | + * **standalone**: Defaults to false. If this is set to true, the `ngModel` will not |
| 140 | + * register itself with its parent form, and will act as if it's not in the form. This |
| 141 | + * can be handy if you have form meta-controls, a.k.a. form elements nested in |
| 142 | + * the `<form>` tag that control the display of the form, but don't contain form data. |
| 143 | + * |
| 144 | + * ```html |
| 145 | + * <form> |
| 146 | + * <input name="login" ngModel placeholder="Login"> |
| 147 | + * <input type="checkbox" ngModel [ngModelOptions]="{standalone: true}"> Show more options? |
| 148 | + * </form> |
| 149 | + * <!-- form value: {login: ''} --> |
| 150 | + * ``` |
| 151 | + * |
| 152 | + * **updateOn**: Defaults to `'change'`. Defines the event upon which the form control |
| 153 | + * value and validity will update. Also accepts `'blur'` and `'submit'`. |
| 154 | + * |
| 155 | + * ```html |
| 156 | + * <input [(ngModel)]="firstName" [ngModelOptions]="{updateOn: 'blur'}"> |
| 157 | + * ``` |
| 158 | + * |
| 159 | + */ |
| 160 | + @Input('ngModelOptions') options: {name?: string, standalone?: boolean, updateOn?: FormHooks}; |
123 | 161 |
|
124 | 162 | @Output('ngModelChange') update = new EventEmitter(); |
125 | 163 |
|
@@ -170,11 +208,18 @@ export class NgModel extends NgControl implements OnChanges, |
170 | 208 | } |
171 | 209 |
|
172 | 210 | private _setUpControl(): void { |
| 211 | + this._setUpdateStrategy(); |
173 | 212 | this._isStandalone() ? this._setUpStandalone() : |
174 | 213 | this.formDirective.addControl(this); |
175 | 214 | this._registered = true; |
176 | 215 | } |
177 | 216 |
|
| 217 | + private _setUpdateStrategy(): void { |
| 218 | + if (this.options && this.options.updateOn != null) { |
| 219 | + this._control._updateOn = this.options.updateOn; |
| 220 | + } |
| 221 | + } |
| 222 | + |
178 | 223 | private _isStandalone(): boolean { |
179 | 224 | return !this._parent || !!(this.options && this.options.standalone); |
180 | 225 | } |
|
0 commit comments