Skip to content

Commit 509e61e

Browse files
committed
[DSC-1831] Fix issue with browser-only directive
1 parent b65866f commit 509e61e

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

src/app/shared/utils/browser-only.directive.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import {
33
ChangeDetectorRef,
44
Directive,
55
Inject,
6-
OnInit,
6+
Input,
7+
OnChanges,
78
PLATFORM_ID,
9+
SimpleChanges,
810
TemplateRef,
911
ViewContainerRef,
1012
} from '@angular/core';
@@ -16,28 +18,32 @@ import {
1618
/**
1719
* Structural Directive for rendering a template reference on client side only
1820
*/
19-
export class BrowserOnlyDirective implements OnInit {
21+
export class BrowserOnlyDirective implements OnChanges {
22+
23+
@Input() dsRenderOnlyForBrowser: boolean;
2024

2125
constructor(
22-
@Inject(PLATFORM_ID) protected platformId: string,
26+
@Inject(PLATFORM_ID) private platformId: string,
2327
private viewContainer: ViewContainerRef,
2428
private changeDetector: ChangeDetectorRef,
2529
private templateRef: TemplateRef<any>,
2630
) {
2731
}
2832

29-
ngOnInit(): void {
30-
this.showTemplateBlockInView();
33+
ngOnChanges(changes: SimpleChanges): void {
34+
if (changes.dsRenderOnlyForBrowser.firstChange && changes.dsRenderOnlyForBrowser.currentValue) {
35+
this.showTemplateBlockInView();
36+
}
3137
}
3238

3339
/**
3440
* Show template in view container according to platform
3541
*/
3642
private showTemplateBlockInView(): void {
43+
this.viewContainer.clear();
3744
if (!this.templateRef) {
3845
return;
3946
}
40-
this.viewContainer.clear();
4147

4248
if (isPlatformBrowser(this.platformId)) {
4349
this.viewContainer.createEmbeddedView(this.templateRef);

0 commit comments

Comments
 (0)