Skip to content

Commit 0ed5d7d

Browse files
authored
fix(material/slider): log proper error when slider isn't configured correctly (#29745)
Fixes that we were throwing a cryptic error if the slider isn't set up properly. Fixes #29738.
1 parent 2dd269e commit 0ed5d7d

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/material/slider/slider-thumb.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,22 @@ export class MatSliderVisualThumb implements _MatSliderVisualThumb, AfterViewIni
110110
}
111111

112112
ngAfterViewInit() {
113+
const sliderInput = this._slider._getInput(this.thumbPosition);
114+
115+
// No-op if the slider isn't configured properly. `MatSlider` will
116+
// throw an error instructing the user how to set up the slider.
117+
if (!sliderInput) {
118+
return;
119+
}
120+
113121
this._ripple.radius = 24;
114-
this._sliderInput = this._slider._getInput(this.thumbPosition)!;
122+
this._sliderInput = sliderInput;
115123
this._sliderInputEl = this._sliderInput._hostElement;
116-
const input = this._sliderInputEl;
117124

118125
// These listeners don't update any data bindings so we bind them outside
119126
// of the NgZone to prevent Angular from needlessly running change detection.
120127
this._ngZone.runOutsideAngular(() => {
128+
const input = this._sliderInputEl!;
121129
input.addEventListener('pointermove', this._onPointerMove);
122130
input.addEventListener('pointerdown', this._onDragStart);
123131
input.addEventListener('pointerup', this._onDragEnd);

src/material/slider/slider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ export class MatSlider implements AfterViewInit, OnDestroy, _MatSlider {
428428
if (typeof ngDevMode === 'undefined' || ngDevMode) {
429429
_validateInputs(
430430
this._isRange,
431-
this._getInput(_MatThumb.END)!,
431+
this._getInput(_MatThumb.END),
432432
this._getInput(_MatThumb.START),
433433
);
434434
}
@@ -938,12 +938,12 @@ export class MatSlider implements AfterViewInit, OnDestroy, _MatSlider {
938938
/** Ensures that there is not an invalid configuration for the slider thumb inputs. */
939939
function _validateInputs(
940940
isRange: boolean,
941-
endInputElement: _MatSliderThumb | _MatSliderRangeThumb,
942-
startInputElement?: _MatSliderThumb,
941+
endInputElement: _MatSliderThumb | _MatSliderRangeThumb | undefined,
942+
startInputElement: _MatSliderThumb | undefined,
943943
): void {
944944
const startValid =
945945
!isRange || startInputElement?._hostElement.hasAttribute('matSliderStartThumb');
946-
const endValid = endInputElement._hostElement.hasAttribute(
946+
const endValid = endInputElement?._hostElement.hasAttribute(
947947
isRange ? 'matSliderEndThumb' : 'matSliderThumb',
948948
);
949949

0 commit comments

Comments
 (0)