From a191e9697c32062eda06cd1f1cfd856d89c16026 Mon Sep 17 00:00:00 2001 From: Kara Erickson Date: Mon, 13 Jun 2016 16:32:45 -0700 Subject: [PATCH] feat(forms): support setting control name in ngModelOptions --- .../common/src/forms/directives/ng_model.ts | 7 ++++--- .../common/test/forms/integration_spec.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/modules/@angular/common/src/forms/directives/ng_model.ts b/modules/@angular/common/src/forms/directives/ng_model.ts index c95cd68f86..a4a5fb19a6 100644 --- a/modules/@angular/common/src/forms/directives/ng_model.ts +++ b/modules/@angular/common/src/forms/directives/ng_model.ts @@ -26,11 +26,10 @@ export const formControlBinding: any = * two-way binding, use `[(ngModel)]` to ensure the model updates in * both directions. * - * ### Example ([live demo](http://plnkr.co/edit/R3UX5qDaUqFO2VYR0UzH?p=preview)) * ```typescript * @Component({ * selector: "search-comp", - * directives: [FORM_DIRECTIVES], + * directives: [], * template: `` * }) * class SearchComp { @@ -55,7 +54,7 @@ export class NgModel extends NgControl implements OnChanges, @Input('ngModel') model: any; @Input() name: string; - + @Input('ngModelOptions') options: {name?: string}; @Output('ngModelChange') update = new EventEmitter(); constructor(@Optional() @Host() private _parent: ControlContainer, @@ -112,6 +111,8 @@ export class NgModel extends NgControl implements OnChanges, } private _checkName() { + if (this.options && this.options.name) this.name = this.options.name; + if (this._parent && !this.name) { throw new BaseException( `Name attribute must be set if ngModel is used within a form. diff --git a/modules/@angular/common/test/forms/integration_spec.ts b/modules/@angular/common/test/forms/integration_spec.ts index f91fdb1fc1..53214614b1 100644 --- a/modules/@angular/common/test/forms/integration_spec.ts +++ b/modules/@angular/common/test/forms/integration_spec.ts @@ -16,6 +16,7 @@ import {ListWrapper} from '../../src/facade/collection'; import {PromiseWrapper} from '../../src/facade/promise'; export function main() { + // TODO(kara): Turn these tests on in CI when we flip the switch on new forms module xdescribe('integration tests', () => { it('should initialize DOM elements with the given form object', @@ -1261,6 +1262,23 @@ export function main() { }); })); + it('should override name attribute with ngModelOptions name if provided', + fakeAsync(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { + const t = ` +
+ +
+ `; + + const fixture = tcb.overrideTemplate(MyComp8, t).createFakeAsync(MyComp8); + tick(); + fixture.debugElement.componentInstance.data = 'some data'; + fixture.detectChanges(); + const form = fixture.debugElement.children[0].inject(NgForm); + + tick(); + expect(form.value).toEqual({two: 'some data'}); + }))); // TODO(kara): Fix when re-doing radio buttons xit('should support ',