Skip to content

Commit fe2a55f

Browse files
committed
fix(elements): cleanup template wrapper on destroy
1 parent 4df51e8 commit fe2a55f

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

projects/igniteui-angular-elements/src/app/custom-strategy.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ class IgxCustomNgElementStrategy extends ComponentNgElementStrategy {
4141
*/
4242
public [ComponentRefKey] = new Promise<ComponentRef<any>>((resolve, _) => this.setComponentRef = resolve);
4343

44-
private _templateWrapper: TemplateWrapperComponent;
44+
private _templateWrapperRef: ComponentRef<TemplateWrapperComponent>;
4545
protected get templateWrapper(): TemplateWrapperComponent {
46-
if (!this._templateWrapper) {
46+
if (!this._templateWrapperRef) {
4747
const componentRef = (this as any).componentRef as ComponentRef<any>;
4848
const viewRef = componentRef.injector.get(ViewContainerRef);
49-
this._templateWrapper = viewRef.createComponent(TemplateWrapperComponent).instance;
49+
this._templateWrapperRef = viewRef.createComponent(TemplateWrapperComponent);
5050
}
51-
return this._templateWrapper;
51+
return this._templateWrapperRef.instance;
5252
}
5353

5454
private _configSelectors: string;
@@ -63,7 +63,7 @@ class IgxCustomNgElementStrategy extends ComponentNgElementStrategy {
6363
super(_componentFactory, _injector);
6464
}
6565

66-
override async initializeComponent(element: HTMLElement) {
66+
protected override async initializeComponent(element: HTMLElement) {
6767
if (!element.isConnected) {
6868
// D.P. 2022-09-20 do not initialize on connectedCallback that is not actually connected
6969
// connectedCallback may be called once your element is no longer connected
@@ -113,15 +113,15 @@ class IgxCustomNgElementStrategy extends ComponentNgElementStrategy {
113113
// ngElementStrategy getter is protected and also has initialization logic, though that should be safe at this point
114114
if (parent?.ngElementStrategy) {
115115
this.angularParent = parent.ngElementStrategy.angularParent;
116-
const parentComponentRef = await parent?.ngElementStrategy[ComponentRefKey];
116+
let parentComponentRef = await parent?.ngElementStrategy[ComponentRefKey];
117117
parentInjector = parentComponentRef?.injector;
118118

119119
// TODO: Consider general solution (as in Parent w/ @igxAnchor tag)
120120
if (element.tagName.toLocaleLowerCase() === 'igc-grid-toolbar'
121121
|| element.tagName.toLocaleLowerCase() === 'igc-paginator') {
122122
// NOPE: viewcontainerRef will re-render this node again, no option for rootNode :S
123123
// this.componentRef = parentAnchor.createComponent(this.componentFactory.componentType, { projectableNodes, injector: childInjector });
124-
const parentComponentRef = await parent?.ngElementStrategy[ComponentRefKey];
124+
parentComponentRef = await parent?.ngElementStrategy[ComponentRefKey];
125125
parentAnchor = parentComponentRef?.instance.anchor;
126126
}
127127
} else if ((parent as any)?.__componentRef) {
@@ -220,6 +220,14 @@ class IgxCustomNgElementStrategy extends ComponentNgElementStrategy {
220220
if (['igc-grid', 'igc-tree-grid', 'igc-hierarchical-grid'].includes(element.tagName.toLocaleLowerCase())) {
221221
this.patchGridPopups();
222222
}
223+
224+
// instead of duplicating super.disconnect() w/ the scheduled destroy:
225+
componentRef.onDestroy(() => {
226+
if (this._templateWrapperRef) {
227+
this._templateWrapperRef.destroy();
228+
this._templateWrapperRef = null;
229+
}
230+
});
223231
}
224232

225233
public override setInputValue(property: string, value: any, transform?: (value: any) => any): void {
@@ -354,17 +362,6 @@ class IgxCustomNgElementStrategy extends ComponentNgElementStrategy {
354362
}
355363
//#endregion schedule query update
356364

357-
/**
358-
* assignTemplateCallback
359-
*/
360-
public assignTemplateCallback(templateProp: string, callback: any) {
361-
const componentRef = (this as any).componentRef as ComponentRef<any>;
362-
if (componentRef) {
363-
const templateRef = this.templateWrapper.addTemplate(callback);
364-
componentRef.instance[templateProp] = templateRef;
365-
componentRef.changeDetectorRef.detectChanges();
366-
}
367-
}
368365

369366
//#region Grid popups hide on scroll
370367
/**
@@ -400,7 +397,7 @@ class IgxCustomNgElementStrategy extends ComponentNgElementStrategy {
400397
}
401398
//#endregion
402399

403-
override disconnect(): void {
400+
public override disconnect(): void {
404401
if (this.angularParent) {
405402
this.angularParent.onDestroy(() => super.disconnect());
406403
} else {

0 commit comments

Comments
 (0)