Skip to content

Commit dcb06a7

Browse files
committed
refactor(material/select): use afterNextRender to wait for options
1 parent 63a764d commit dcb06a7

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/material/select/select.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ import {
6161
booleanAttribute,
6262
inject,
6363
numberAttribute,
64+
afterNextRender,
65+
runInInjectionContext,
66+
Injector,
6467
} from '@angular/core';
6568
import {
6669
AbstractControl,
@@ -553,10 +556,14 @@ export class MatSelect
553556
);
554557
}
555558

556-
return this._ngZone.onStable.pipe(
557-
take(1),
558-
switchMap(() => this.optionSelectionChanges),
559-
);
559+
return new Observable(subscriber =>
560+
runInInjectionContext(this._injector, () => {
561+
afterNextRender(() => {
562+
subscriber.next();
563+
subscriber.complete();
564+
});
565+
}),
566+
).pipe(switchMap(() => this.optionSelectionChanges));
560567
}) as Observable<MatOptionSelectionChange>;
561568

562569
/** Event emitted when the select panel has been toggled. */
@@ -584,10 +591,16 @@ export class MatSelect
584591
*/
585592
@Output() readonly valueChange: EventEmitter<any> = new EventEmitter<any>();
586593

594+
private _injector = inject(Injector);
595+
587596
constructor(
588597
protected _viewportRuler: ViewportRuler,
589598
protected _changeDetectorRef: ChangeDetectorRef,
590-
protected _ngZone: NgZone,
599+
/**
600+
* @deprecated Unused param, will be removed.
601+
* @breaking-change 19.0.0
602+
*/
603+
_unusedNgZone: NgZone,
591604
defaultErrorStateMatcher: ErrorStateMatcher,
592605
readonly _elementRef: ElementRef,
593606
@Optional() private _dir: Directionality,

tools/public_api_guard/material/select.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export { MatPrefix }
8686

8787
// @public (undocumented)
8888
export class MatSelect implements AfterContentInit, OnChanges, OnDestroy, OnInit, DoCheck, ControlValueAccessor, MatFormFieldControl<any> {
89-
constructor(_viewportRuler: ViewportRuler, _changeDetectorRef: ChangeDetectorRef, _ngZone: NgZone, defaultErrorStateMatcher: ErrorStateMatcher, _elementRef: ElementRef, _dir: Directionality, parentForm: NgForm, parentFormGroup: FormGroupDirective, _parentFormField: MatFormField, ngControl: NgControl, tabIndex: string, scrollStrategyFactory: any, _liveAnnouncer: LiveAnnouncer, _defaultOptions?: MatSelectConfig | undefined);
89+
constructor(_viewportRuler: ViewportRuler, _changeDetectorRef: ChangeDetectorRef,
90+
_unusedNgZone: NgZone, defaultErrorStateMatcher: ErrorStateMatcher, _elementRef: ElementRef, _dir: Directionality, parentForm: NgForm, parentFormGroup: FormGroupDirective, _parentFormField: MatFormField, ngControl: NgControl, tabIndex: string, scrollStrategyFactory: any, _liveAnnouncer: LiveAnnouncer, _defaultOptions?: MatSelectConfig | undefined);
9091
ariaLabel: string;
9192
ariaLabelledby: string;
9293
protected _canOpen(): boolean;
@@ -153,8 +154,6 @@ export class MatSelect implements AfterContentInit, OnChanges, OnDestroy, OnInit
153154
ngOnDestroy(): void;
154155
// (undocumented)
155156
ngOnInit(): void;
156-
// (undocumented)
157-
protected _ngZone: NgZone;
158157
_onAttached(): void;
159158
_onBlur(): void;
160159
_onChange: (value: any) => void;

0 commit comments

Comments
 (0)