Skip to content

Commit 4b74490

Browse files
committed
2 parents 04e5dfc + c2eaef5 commit 4b74490

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

nativescript-angular/common/detached-loader.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ComponentRef, ComponentFactory, ViewContainerRef, Component, Type, ComponentFactoryResolver, ChangeDetectorRef } from '@angular/core';
1+
import { ComponentRef, ComponentFactory, ViewContainerRef, Component, Type, ComponentFactoryResolver, ChangeDetectorRef, ApplicationRef, OnDestroy } from '@angular/core';
22
import { Trace } from '@nativescript/core';
33

44
/**
@@ -10,13 +10,20 @@ import { Trace } from '@nativescript/core';
1010
selector: 'DetachedContainer',
1111
template: `<Placeholder #loader></Placeholder>`,
1212
})
13-
export class DetachedLoader {
13+
export class DetachedLoader implements OnDestroy {
14+
private disposeFunctions: Array<() => void> = [];
1415
// tslint:disable-line:component-class-suffix
15-
constructor(private resolver: ComponentFactoryResolver, private changeDetector: ChangeDetectorRef, private containerRef: ViewContainerRef) {}
16+
constructor(private resolver: ComponentFactoryResolver, private changeDetector: ChangeDetectorRef, private containerRef: ViewContainerRef, private appRef: ApplicationRef) {}
1617

1718
private loadInLocation(componentType: Type<any>): Promise<ComponentRef<any>> {
1819
const factory = this.resolver.resolveComponentFactory(componentType);
19-
const componentRef = this.containerRef.createComponent(factory, this.containerRef.length, this.containerRef.injector);
20+
const componentRef = factory.create(this.containerRef.injector);
21+
this.appRef.attachView(componentRef.hostView);
22+
23+
this.disposeFunctions.push(() => {
24+
this.appRef.detachView(componentRef.hostView);
25+
componentRef.destroy();
26+
});
2027

2128
// Component is created, built may not be checked if we are loading
2229
// inside component with OnPush CD strategy. Mark us for check to be sure CD will reach us.
@@ -27,6 +34,10 @@ export class DetachedLoader {
2734
return Promise.resolve(componentRef);
2835
}
2936

37+
public ngOnDestroy() {
38+
this.disposeFunctions.forEach((fn) => fn());
39+
}
40+
3041
public detectChanges() {
3142
this.changeDetector.markForCheck();
3243
}

nativescript-angular/renderer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export class NativeScriptRenderer extends Renderer2 {
3131
}
3232

3333
@profile
34-
insertBefore(parent: NgView, newChild: NgView, { previous, next }: ElementReference): void {
34+
insertBefore(parent: NgView, newChild: NgView, refChild: NgView | ElementReference): void {
35+
let { previous, next } = refChild instanceof View ? this.nextSibling(refChild) : refChild;
3536
if (NativeScriptDebug.isLogEnabled()) {
3637
NativeScriptDebug.rendererLog(`NativeScriptRenderer.insertBefore child: ${newChild} ` + `parent: ${parent} previous: ${previous} next: ${next}`);
3738
}

0 commit comments

Comments
 (0)