diff --git a/modules/angular2/src/common/forms/directives/select_control_value_accessor.ts b/modules/angular2/src/common/forms/directives/select_control_value_accessor.ts index e10346cc0b..bada98ae8c 100644 --- a/modules/angular2/src/common/forms/directives/select_control_value_accessor.ts +++ b/modules/angular2/src/common/forms/directives/select_control_value_accessor.ts @@ -15,6 +15,7 @@ import { StringWrapper, isPrimitive, isPresent, + isBlank, looseIdentical } from 'angular2/src/facade/lang'; @@ -24,6 +25,7 @@ const SELECT_VALUE_ACCESSOR = CONST_EXPR(new Provider( NG_VALUE_ACCESSOR, {useExisting: forwardRef(() => SelectControlValueAccessor), multi: true})); function _buildValueString(id: string, value: any): string { + if (isBlank(id)) return `${value}`; if (!isPrimitive(value)) value = "Object"; return StringWrapper.slice(`${id}: ${value}`, 0, 50); } @@ -70,7 +72,10 @@ export class SelectControlValueAccessor implements ControlValueAccessor { return null; } - _getOptionValue(valueString: string): any { return this._optionMap.get(_extractId(valueString)); } + _getOptionValue(valueString: string): any { + let value = this._optionMap.get(_extractId(valueString)); + return isPresent(value) ? value : valueString; + } } /** @@ -93,14 +98,21 @@ export class NgSelectOption implements OnDestroy { if (isPresent(this._select)) this.id = this._select._registerOption(); } - @Input() - set value(value: any) { + @Input('ng-value') + set ngValue(value: any) { if (this._select == null) return; this._select._optionMap.set(this.id, value); this._setElementValue(_buildValueString(this.id, value)); this._select.writeValue(this._select.value); } + @Input('value') + set value(value: any) { + if (this._select == null) return; + this._setElementValue(value); + this._select.writeValue(this._select.value); + } + _setElementValue(value: string): void { this._renderer.setElementProperty(this._element.nativeElement, 'value', value); } diff --git a/modules/angular2/test/common/forms/integration_spec.ts b/modules/angular2/test/common/forms/integration_spec.ts index 6c07e4d6f5..ba76d390b5 100644 --- a/modules/angular2/test/common/forms/integration_spec.ts +++ b/modules/angular2/test/common/forms/integration_spec.ts @@ -396,10 +396,11 @@ export function main() { var select = fixture.debugElement.query(By.css("select")); var sfOption = fixture.debugElement.query(By.css("option")); - expect(select.nativeElement.value).toEqual("0: SF"); + + expect(select.nativeElement.value).toEqual("SF"); expect(sfOption.nativeElement.selected).toBe(true); - select.nativeElement.value = "1: NYC"; + select.nativeElement.value = "NYC"; dispatchEvent(select.nativeElement, "input"); expect(fixture.debugElement.componentInstance.form.value).toEqual({"city": 'NYC'}); @@ -423,12 +424,13 @@ export function main() { fixture.debugElement.componentInstance.form = new ControlGroup({"city": new Control("NYC")}); + fixture.debugElement.componentInstance.data = ['SF', 'NYC']; fixture.detectChanges(); tick(); var select = fixture.debugElement.query(By.css("select")); - expect(select.nativeElement.value).toEqual("1: NYC"); + expect(select.nativeElement.value).toEqual("NYC"); }))); it("with option values that are objects", @@ -436,7 +438,7 @@ export function main() { (tcb: TestComponentBuilder, async) => { var t = `