diff --git a/src/positioning/positioning.service.ts b/src/positioning/positioning.service.ts index d251c49046..bf3780861b 100644 --- a/src/positioning/positioning.service.ts +++ b/src/positioning/positioning.service.ts @@ -1,6 +1,8 @@ -import { Injectable, ElementRef, RendererFactory2 } from '@angular/core'; +import { Injectable, ElementRef, RendererFactory2, Inject, PLATFORM_ID } from '@angular/core'; +import { isPlatformBrowser } from '@angular/common'; import { positionElements } from './ng-positioning'; + import { fromEvent, merge, of, animationFrameScheduler, Subject } from 'rxjs'; import { Options } from './models'; @@ -43,31 +45,33 @@ export interface PositioningOptions { export class PositioningService { options: Options; private update$$ = new Subject(); - - private events$: any = merge( - fromEvent(window, 'scroll'), - fromEvent(window, 'resize'), - of(0, animationFrameScheduler), - this.update$$ - ); - private positionElements = new Map(); - constructor(rendererFactory: RendererFactory2) { - this.events$ - .subscribe(() => { - this.positionElements - .forEach((positionElement: PositioningOptions) => { - positionElements( - _getHtmlElement(positionElement.target), - _getHtmlElement(positionElement.element), - positionElement.attachment, - positionElement.appendToBody, - this.options, - rendererFactory.createRenderer(null, null) - ); - }); - }); + constructor( + rendererFactory: RendererFactory2, + @Inject(PLATFORM_ID) platformId: number + ) { + if (isPlatformBrowser(platformId)) { + merge( + fromEvent(window, 'scroll'), + fromEvent(window, 'resize'), + of(0, animationFrameScheduler), + this.update$$ + ) + .subscribe(() => { + this.positionElements + .forEach((positionElement: PositioningOptions) => { + positionElements( + _getHtmlElement(positionElement.target), + _getHtmlElement(positionElement.element), + positionElement.attachment, + positionElement.appendToBody, + this.options, + rendererFactory.createRenderer(null, null) + ); + }); + }); + } } position(options: PositioningOptions): void {