Skip to content

Commit 65e659f

Browse files
eitanp461tocker
authored andcommitted
Avoid errors in angular-universal server rendering
1 parent ec23f16 commit 65e659f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/cloudinary-background-image.directive.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ export class CloudinaryBackgroundImageDirective implements AfterViewInit {
1515
constructor(private renderer: Renderer2, private el: ElementRef, private cloudinary: Cloudinary) {
1616
}
1717

18+
isBrowser(): boolean {
19+
return typeof window !== 'undefined';
20+
}
21+
1822
ngAfterViewInit() {
19-
const nativeElement = this.el.nativeElement;
20-
const options = this.cloudinary.toCloudinaryAttributes(nativeElement.attributes, this.transformations);
21-
const imageUrl = this.cloudinary.url(this.clBackgroundImage, options);
22-
this.renderer.setStyle(nativeElement, 'background-image', `url('${imageUrl}')`);
23-
this.renderer.setStyle(nativeElement, 'background-repeat', 'no-repeat');
24-
this.renderer.setStyle(nativeElement, 'background-position', 'center');
23+
if (this.isBrowser()) {
24+
const nativeElement = this.el.nativeElement;
25+
const options = this.cloudinary.toCloudinaryAttributes(nativeElement.attributes, this.transformations);
26+
const imageUrl = this.cloudinary.url(this.clBackgroundImage, options);
27+
this.renderer.setStyle(nativeElement, 'background-image', `url('${imageUrl}')`);
28+
this.renderer.setStyle(nativeElement, 'background-repeat', 'no-repeat');
29+
this.renderer.setStyle(nativeElement, 'background-position', 'center');
30+
}
2531
}
2632
}

0 commit comments

Comments
 (0)