Skip to content

Commit e1e44a9

Browse files
committed
fix(select): set value individually from ngModel
Closes angular#7975 Closes angular#7978
1 parent f371c90 commit e1e44a9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

modules/angular2/src/common/forms/directives/select_control_value_accessor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ export class NgSelectOption implements OnDestroy {
113113

114114
@Input('value')
115115
set value(value: any) {
116-
if (this._select == null) return;
117116
this._setElementValue(value);
118-
this._select.writeValue(this._select.value);
117+
if (isPresent(this._select)) this._select.writeValue(this._select.value);
119118
}
120119

121120
/** @internal */

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,30 @@ export function main() {
378378
});
379379
}));
380380

381+
it("with basic selection and value bindings",
382+
inject([TestComponentBuilder, AsyncTestCompleter],
383+
(tcb: TestComponentBuilder, async) => {
384+
var t = `<select>
385+
<option *ngFor="#city of list" [value]="city['id']">
386+
{{ city['name'] }}
387+
</option>
388+
</select>`;
389+
390+
tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((fixture) => {
391+
var testComp = fixture.debugElement.componentInstance;
392+
testComp.list = [{"id": "0", "name": "SF"}, {"id": "1", "name": "NYC"}];
393+
fixture.detectChanges();
394+
395+
var sfOption = fixture.debugElement.query(By.css("option"));
396+
expect(sfOption.nativeElement.value).toEqual('0');
397+
398+
testComp.list[0]['id'] = '2';
399+
fixture.detectChanges();
400+
expect(sfOption.nativeElement.value).toEqual('2');
401+
async.done();
402+
});
403+
}));
404+
381405
it("with ngControl",
382406
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder,
383407
async) => {

0 commit comments

Comments
 (0)