Skip to content

Commit

Permalink
fix(forms): update accessor value when native select value changes
Browse files Browse the repository at this point in the history
Closes #8710
  • Loading branch information
kara committed May 25, 2016
1 parent 1ac38bd commit 7a2ce7f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
}

registerOnChange(fn: (value: any) => any): void {
this.onChange = (valueString: string) => { fn(this._getOptionValue(valueString)); };
this.onChange = (valueString: string) => {
this.value = valueString;
fn(this._getOptionValue(valueString));
};
}
registerOnTouched(fn: () => any): void { this.onTouched = fn; }

Expand Down
35 changes: 34 additions & 1 deletion modules/@angular/common/test/forms/integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ export function main() {
});
}));

it("when new options are added",
it("when new options are added (selection through the model)",
inject([TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async) => {
var t = `<div>
Expand Down Expand Up @@ -615,6 +615,39 @@ export function main() {
});
}));

it("when new options are added (selection through the UI)",
inject([TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async) => {
var t = `<div>
<select [(ngModel)]="selectedCity">
<option *ngFor="let c of list" [ngValue]="c">{{c['name']}}</option>
</select>
</div>`;

tcb.overrideTemplate(MyComp8, t)
.createAsync(MyComp8)
.then((fixture) => {

var testComp: MyComp8 = fixture.debugElement.componentInstance;
testComp.list = [{"name": "SF"}, {"name": "NYC"}];
testComp.selectedCity = testComp.list[0];
fixture.detectChanges();

var select = fixture.debugElement.query(By.css("select"));
var ny = fixture.debugElement.queryAll(By.css("option"))[1];

select.nativeElement.value = "1: Object";
dispatchEvent(select.nativeElement, "change");
testComp.list.push({"name": "Buffalo"});
fixture.detectChanges();

expect(select.nativeElement.value).toEqual("1: Object");
expect(ny.nativeElement.selected).toBe(true);
async.done();
});
}));


it("when options are removed",
inject([TestComponentBuilder, AsyncTestCompleter],
(tcb: TestComponentBuilder, async) => {
Expand Down

0 comments on commit 7a2ce7f

Please sign in to comment.