Skip to content

Commit 3b578cf

Browse files
committed
fix(angular/select): component value not in sync with control value on init
Fixes the `SbbSelect.value` property not being in sync with the `ControlValueAccessor` initial value on init. angular/components#18443
1 parent ee942d0 commit 3b578cf

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/angular/select/select/select.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ class BasicSelectOnPush {
334334
`,
335335
})
336336
class BasicSelectOnPushPreselected {
337+
@ViewChild(SbbSelect) select: SbbSelect;
337338
foods: any[] = [
338339
{ value: 'steak-0', viewValue: 'Steak' },
339340
{ value: 'pizza-1', viewValue: 'Pizza' },
@@ -3769,6 +3770,15 @@ describe('SbbSelect', () => {
37693770

37703771
expect(select.textContent).not.toContain('Pizza');
37713772
}));
3773+
3774+
it('should sync up the form control value with the component value', fakeAsync(() => {
3775+
const fixture = TestBed.createComponent(BasicSelectOnPushPreselected);
3776+
fixture.detectChanges();
3777+
flush();
3778+
3779+
expect(fixture.componentInstance.control.value).toBe('pizza-1');
3780+
expect(fixture.componentInstance.select.value).toBe('pizza-1');
3781+
}));
37723782
});
37733783

37743784
describe('when resetting the value by setting null or undefined', () => {

src/angular/select/select/select.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,11 @@ export class SbbSelect
855855
// Defer setting the value in order to avoid the "Expression
856856
// has changed after it was checked" errors from Angular.
857857
Promise.resolve().then(() => {
858-
this._setSelectionByValue(this.ngControl ? this.ngControl.value : this._value);
858+
if (this.ngControl) {
859+
this._value = this.ngControl.value;
860+
}
861+
862+
this._setSelectionByValue(this._value);
859863
this.stateChanges.next();
860864
});
861865
}

0 commit comments

Comments
 (0)