fix(forms): do not reset the value of the input when it came from the view
This commit is contained in:
@@ -721,6 +721,33 @@ export function main() {
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
describe("ng-model corner cases", () => {
|
||||
it("should not update the view when the value initially came from the view",
|
||||
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
||||
var form = new Control("");
|
||||
|
||||
var t =
|
||||
`<div><input type="text" [ng-form-control]="form" [(ng-model)]="name"></div>`;
|
||||
var rootTC;
|
||||
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then(
|
||||
(root) => { rootTC = root; });
|
||||
tick();
|
||||
rootTC.componentInstance.form = form;
|
||||
rootTC.detectChanges();
|
||||
|
||||
var input = rootTC.query(By.css("input")).nativeElement;
|
||||
input.value = "aa";
|
||||
input.selectionStart = 1;
|
||||
dispatchEvent(input, "change");
|
||||
|
||||
tick();
|
||||
rootTC.detectChanges();
|
||||
|
||||
// selection start has not changed because we did not reset the value
|
||||
expect(input.selectionStart).toEqual(1);
|
||||
})));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -71,6 +71,15 @@ export function main() {
|
||||
expect(onChange).toEqual(["invoked", "newValue"]);
|
||||
});
|
||||
|
||||
it("should not invoke on change when explicitly specified", () => {
|
||||
var onChange = null;
|
||||
c.registerOnChange((v) => onChange = ["invoked", v]);
|
||||
|
||||
c.updateValue("newValue", {emitModelToViewChange: false});
|
||||
|
||||
expect(onChange).toBeNull();
|
||||
});
|
||||
|
||||
it("should update the parent", () => {
|
||||
c.updateValue("newValue");
|
||||
expect(g.value).toEqual({"one": "newValue"});
|
||||
|
||||
Reference in New Issue
Block a user