8
8
9
9
import { Directionality } from '@angular/cdk/bidi' ;
10
10
import { coerceBooleanProperty , coerceNumberProperty } from '@angular/cdk/coercion' ;
11
- import { normalizePassiveListenerOptions } from '@angular/cdk/platform' ;
11
+ import { normalizePassiveListenerOptions , Platform } from '@angular/cdk/platform' ;
12
12
import {
13
13
AfterViewInit ,
14
14
Attribute ,
@@ -322,8 +322,11 @@ export class MatSlider implements AfterViewInit, OnChanges, OnDestroy, ControlVa
322
322
@ViewChild ( 'trackMarker' , { static : false } ) _trackMarker : ElementRef < HTMLElement > ;
323
323
324
324
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 ,
327
330
@Attribute ( 'tabindex' ) tabIndex : string ,
328
331
@Optional ( ) @Inject ( ANIMATION_MODULE_TYPE ) public _animationMode ?: string ) {
329
332
this . tabIndex = parseInt ( tabIndex ) || 0 ;
@@ -342,19 +345,28 @@ export class MatSlider implements AfterViewInit, OnChanges, OnDestroy, ControlVa
342
345
343
346
ngAfterViewInit ( ) {
344
347
this . _isInitialized = true ;
345
- this . _foundation . init ( ) ;
346
348
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
+ }
353
369
354
- this . _syncStep ( ) ;
355
- this . _syncValue ( ) ;
356
- this . _syncMax ( ) ;
357
- this . _syncMin ( ) ;
358
370
this . _syncDisabled ( ) ;
359
371
}
360
372
@@ -385,7 +397,11 @@ export class MatSlider implements AfterViewInit, OnChanges, OnDestroy, ControlVa
385
397
386
398
ngOnDestroy ( ) {
387
399
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
+ }
389
405
}
390
406
391
407
/** Focuses the slider. */
0 commit comments