Skip to content

Commit

Permalink
feat(lib): public method to destroy embedded view
Browse files Browse the repository at this point in the history
  • Loading branch information
NagornovAlex committed May 20, 2020
1 parent 54fc6db commit 68cc6b7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
ComponentFactoryResolver,
ChangeDetectorRef,
Renderer2,
Inject
Inject,
EmbeddedViewRef
} from '@angular/core';
import { DOCUMENT } from '@angular/common';

Expand All @@ -33,6 +34,8 @@ export class LazyElementDynamicDirective implements OnInit {
>; // tslint:disable-line:no-input-rename
@Input('axLazyElementDynamicModule') isModule: boolean | undefined; // tslint:disable-line:no-input-rename

private viewRef: EmbeddedViewRef<any> = null;

constructor(
@Inject(DOCUMENT) private document: Document,
private renderer: Renderer2,
Expand Down Expand Up @@ -75,7 +78,7 @@ export class LazyElementDynamicDirective implements OnInit {
}
return this.document.createElement(name);
};
this.vcr.createEmbeddedView(this.template);
this.viewRef = this.vcr.createEmbeddedView(this.template);
this.renderer.createElement = originalCreateElement;
this.cdr.markForCheck();
})
Expand All @@ -98,4 +101,12 @@ export class LazyElementDynamicDirective implements OnInit {
}
});
}

destroyEmbeddedView() {
if (this.viewRef && !this.viewRef.destroyed) {
this.viewRef.detach();
this.viewRef.destroy();
this.viewRef = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
TemplateRef,
ViewContainerRef,
ComponentFactoryResolver,
ChangeDetectorRef
ChangeDetectorRef,
EmbeddedViewRef
} from '@angular/core';

import {
Expand All @@ -24,6 +25,8 @@ export class LazyElementDirective implements OnInit {
@Input('axLazyElementErrorTemplate') errorTemplateRef: TemplateRef<any>; // tslint:disable-line:no-input-rename
@Input('axLazyElementModule') isModule: boolean | undefined; // tslint:disable-line:no-input-rename

private viewRef: EmbeddedViewRef<any> = null;

constructor(
private vcr: ViewContainerRef,
private template: TemplateRef<any>,
Expand Down Expand Up @@ -55,7 +58,7 @@ export class LazyElementDirective implements OnInit {
.loadElement(this.url, elementTag, this.isModule, elementConfig?.hooks)
.then(() => {
this.vcr.clear();
this.vcr.createEmbeddedView(this.template);
this.viewRef = this.vcr.createEmbeddedView(this.template);
this.cdr.markForCheck();
})
.catch(() => {
Expand All @@ -76,4 +79,12 @@ export class LazyElementDirective implements OnInit {
}
});
}

destroyEmbeddedView() {
if (this.viewRef && !this.viewRef.destroyed) {
this.viewRef.detach();
this.viewRef.destroy();
this.viewRef = null;
}
}
}

0 comments on commit 68cc6b7

Please sign in to comment.