fix(forms): Update NgModel's viewModel when model changes

Closes #3627
This commit is contained in:
Matt Greenland 2015-08-18 21:42:11 -07:00 committed by vsavkin
parent f14b212dc9
commit e36966b83c
2 changed files with 34 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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 = `<input type="text" [(ng-model)]="name">`;
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");
})));
});
});
}