Skip to content

Commit e36966b

Browse files
mgreenlandvsavkin
authored andcommitted
fix(forms): Update NgModel's viewModel when model changes
Closes #3627
1 parent f14b212 commit e36966b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

modules/angular2/src/core/forms/directives/ng_model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class NgModel extends NgControl implements OnChanges {
5757

5858
if (isPropertyUpdated(c, this.viewModel)) {
5959
this._control.updateValue(this.model);
60+
this.viewModel = this.model;
6061
}
6162
}
6263

modules/angular2/test/core/forms/integration_spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,39 @@ export function main() {
756756
// selection start has not changed because we did not reset the value
757757
expect(input.selectionStart).toEqual(1);
758758
})));
759+
760+
it("should update the view when the model is set back to what used to be in the view",
761+
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
762+
var t = `<input type="text" [(ng-model)]="name">`;
763+
var rootTC;
764+
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
765+
(root) => { rootTC = root; });
766+
tick();
767+
rootTC.componentInstance.name = "";
768+
rootTC.detectChanges();
769+
770+
// Type "aa" into the input.
771+
var input = rootTC.query(By.css("input")).nativeElement;
772+
input.value = "aa";
773+
input.selectionStart = 1;
774+
dispatchEvent(input, "change");
775+
776+
tick();
777+
rootTC.detectChanges();
778+
expect(rootTC.componentInstance.name).toEqual("aa");
779+
780+
// Programatically update the input value to be "bb".
781+
rootTC.componentInstance.name = "bb";
782+
tick();
783+
rootTC.detectChanges();
784+
expect(input.value).toEqual("bb");
785+
786+
// Programatically set it back to "aa".
787+
rootTC.componentInstance.name = "aa";
788+
tick();
789+
rootTC.detectChanges();
790+
expect(input.value).toEqual("aa");
791+
})));
759792
});
760793
});
761794
}

0 commit comments

Comments
 (0)