88
99import { Directionality } from '@angular/cdk/bidi' ;
1010import { coerceBooleanProperty , coerceNumberProperty } from '@angular/cdk/coercion' ;
11- import { normalizePassiveListenerOptions } from '@angular/cdk/platform' ;
11+ import { normalizePassiveListenerOptions , Platform } from '@angular/cdk/platform' ;
1212import {
1313 AfterViewInit ,
1414 Attribute ,
@@ -322,8 +322,11 @@ export class MatSlider implements AfterViewInit, OnChanges, OnDestroy, ControlVa
322322 @ViewChild ( 'trackMarker' , { static : false } ) _trackMarker : ElementRef < HTMLElement > ;
323323
324324 constructor (
325- private _elementRef : ElementRef < HTMLElement > , private _changeDetectorRef : ChangeDetectorRef ,
326- private _ngZone : NgZone , @Optional ( ) private _dir : Directionality ,
325+ private _elementRef : ElementRef < HTMLElement > ,
326+ private _changeDetectorRef : ChangeDetectorRef ,
327+ private _ngZone : NgZone ,
328+ private _platform : Platform ,
329+ @Optional ( ) private _dir : Directionality ,
327330 @Attribute ( 'tabindex' ) tabIndex : string ,
328331 @Optional ( ) @Inject ( ANIMATION_MODULE_TYPE ) public _animationMode ?: string ) {
329332 this . tabIndex = parseInt ( tabIndex ) || 0 ;
@@ -342,19 +345,28 @@ export class MatSlider implements AfterViewInit, OnChanges, OnDestroy, ControlVa
342345
343346 ngAfterViewInit ( ) {
344347 this . _isInitialized = true ;
345- this . _foundation . init ( ) ;
346348
347- // The standard Angular Material slider is always using discrete values. We always
348- // want to enable discrete values and support ticks, but want to still provide
349- // non-discrete slider visual looks if thumb label is disabled.
350- // TODO(devversion): check if we can get a public API for this.
351- // Tracked with: https://github.com/material-components/material-components-web/issues/5020
352- ( this . _foundation as any ) . isDiscrete_ = true ;
349+ if ( this . _platform . isBrowser ) {
350+ // The MDC slider foundation accesses DOM globals, so we cannot initialize the
351+ // foundation on the server. The foundation would be needed to move the thumb
352+ // to the proper position and to render the ticks.
353+ this . _foundation . init ( ) ;
354+
355+ // The standard Angular Material slider is always using discrete values. We always
356+ // want to enable discrete values and support ticks, but want to still provide
357+ // non-discrete slider visual looks if thumb label is disabled.
358+ // TODO(devversion): check if we can get a public API for this.
359+ // Tracked with: https://github.com/material-components/material-components-web/issues/5020
360+ ( this . _foundation as any ) . isDiscrete_ = true ;
361+
362+ // These bindings cannot be synced in the foundation, as the foundation is not
363+ // initialized and they cause DOM globals to be accessed (to move the thumb)
364+ this . _syncStep ( ) ;
365+ this . _syncValue ( ) ;
366+ this . _syncMax ( ) ;
367+ this . _syncMin ( ) ;
368+ }
353369
354- this . _syncStep ( ) ;
355- this . _syncValue ( ) ;
356- this . _syncMax ( ) ;
357- this . _syncMin ( ) ;
358370 this . _syncDisabled ( ) ;
359371 }
360372
@@ -385,7 +397,11 @@ export class MatSlider implements AfterViewInit, OnChanges, OnDestroy, ControlVa
385397
386398 ngOnDestroy ( ) {
387399 this . _dirChangeSubscription . unsubscribe ( ) ;
388- this . _foundation . destroy ( ) ;
400+ // The foundation cannot be destroyed on the server, as the foundation
401+ // has not be initialized on the server.
402+ if ( this . _platform . isBrowser ) {
403+ this . _foundation . destroy ( ) ;
404+ }
389405 }
390406
391407 /** Focuses the slider. */
0 commit comments