From e36966b83ce2cff30365452f74be1f6623ea90d6 Mon Sep 17 00:00:00 2001 From: Matt Greenland Date: Tue, 18 Aug 2015 21:42:11 -0700 Subject: [PATCH] fix(forms): Update NgModel's viewModel when model changes Closes #3627 --- .../src/core/forms/directives/ng_model.ts | 1 + .../test/core/forms/integration_spec.ts | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/modules/angular2/src/core/forms/directives/ng_model.ts b/modules/angular2/src/core/forms/directives/ng_model.ts index 882e5c729f..14087e9058 100644 --- a/modules/angular2/src/core/forms/directives/ng_model.ts +++ b/modules/angular2/src/core/forms/directives/ng_model.ts @@ -57,6 +57,7 @@ export class NgModel extends NgControl implements OnChanges { if (isPropertyUpdated(c, this.viewModel)) { this._control.updateValue(this.model); + this.viewModel = this.model; } } diff --git a/modules/angular2/test/core/forms/integration_spec.ts b/modules/angular2/test/core/forms/integration_spec.ts index 18d6c84f7e..478e2ab518 100644 --- a/modules/angular2/test/core/forms/integration_spec.ts +++ b/modules/angular2/test/core/forms/integration_spec.ts @@ -756,6 +756,39 @@ export function main() { // selection start has not changed because we did not reset the value expect(input.selectionStart).toEqual(1); }))); + + it("should update the view when the model is set back to what used to be in the view", + inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => { + var t = ``; + var rootTC; + tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then( + (root) => { rootTC = root; }); + tick(); + rootTC.componentInstance.name = ""; + rootTC.detectChanges(); + + // Type "aa" into the input. + var input = rootTC.query(By.css("input")).nativeElement; + input.value = "aa"; + input.selectionStart = 1; + dispatchEvent(input, "change"); + + tick(); + rootTC.detectChanges(); + expect(rootTC.componentInstance.name).toEqual("aa"); + + // Programatically update the input value to be "bb". + rootTC.componentInstance.name = "bb"; + tick(); + rootTC.detectChanges(); + expect(input.value).toEqual("bb"); + + // Programatically set it back to "aa". + rootTC.componentInstance.name = "aa"; + tick(); + rootTC.detectChanges(); + expect(input.value).toEqual("aa"); + }))); }); }); }