Skip to content

Commit 07caa74

Browse files
committed
fix(page-router-outlet): manually run detect changes when navigating to
new page
1 parent 9d335b5 commit 07caa74

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Diff for: nativescript-angular/common/detached-loader.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ export class DetachedLoader { // tslint:disable-line:component-class-suffix
3636
// We are inside a promise here so no need for setTimeout - CD should trigger
3737
// after the promise.
3838
log("DetachedLoader.loadInLocation component loaded -> markForCheck");
39-
this.changeDetector.markForCheck();
4039

4140
return Promise.resolve(componentRef);
4241
}
4342

4443
public detectChanges() {
45-
this.changeDetector.markForCheck();
44+
this.changeDetector.detectChanges();
4645
}
4746

4847
// TODO: change this API -- async promises not needed here anymore.

Diff for: nativescript-angular/router/page-router-outlet.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Attribute, ComponentFactory, ComponentRef, Directive,
3-
ReflectiveInjector, ResolvedReflectiveProvider, ViewContainerRef,
3+
ReflectiveInjector, ResolvedReflectiveProvider,
4+
ViewRef, ViewContainerRef,
45
Inject, ComponentFactoryResolver, Injector
56
} from "@angular/core";
67
import { isPresent } from "../lang-facade";
@@ -61,6 +62,7 @@ export class PageRouterOutlet { // tslint:disable-line:directive-class-suffix
6162
private viewUtil: ViewUtil;
6263
private refCache: RefCache = new RefCache();
6364
private isInitialPage: boolean = true;
65+
private previousPagesViews: Array<ViewRef> = new Array();
6466
private detachedLoaderFactory: ComponentFactory<DetachedLoader>;
6567

6668
private currentActivatedComp: ComponentRef<any>;
@@ -152,8 +154,16 @@ export class PageRouterOutlet { // tslint:disable-line:directive-class-suffix
152154
this.currentActivatedRoute = activatedRoute;
153155

154156
if (this.locationStrategy._isPageNavigatingBack()) {
157+
if (this.previousPagesViews.length) {
158+
this.containerRef.insert(this.previousPagesViews.pop());
159+
}
160+
155161
this.activateOnGoBack(activatedRoute, outletMap);
156162
} else {
163+
if (this.containerRef.length) {
164+
this.previousPagesViews.push(this.containerRef.detach());
165+
}
166+
157167
this.activateOnGoForward(activatedRoute, providers, outletMap, resolver, injector);
158168
}
159169
}
@@ -176,6 +186,9 @@ export class PageRouterOutlet { // tslint:disable-line:directive-class-suffix
176186
const inj = ReflectiveInjector.fromResolvedProviders(providers, injector);
177187
this.currentActivatedComp = this.containerRef.createComponent(
178188
factory, this.containerRef.length, inj, []);
189+
190+
this.currentActivatedComp.changeDetectorRef.detectChanges();
191+
179192
this.refCache.push(this.currentActivatedComp, pageRoute, outletMap, null);
180193

181194
} else {
@@ -194,7 +207,12 @@ export class PageRouterOutlet { // tslint:disable-line:directive-class-suffix
194207
const loaderRef = this.containerRef.createComponent(
195208
this.detachedLoaderFactory, this.containerRef.length, childInjector, []);
196209

210+
loaderRef.changeDetectorRef.detectChanges();
211+
197212
this.currentActivatedComp = loaderRef.instance.loadWithFactory(factory);
213+
214+
this.currentActivatedComp.changeDetectorRef.detectChanges();
215+
198216
this.loadComponentInPage(page, this.currentActivatedComp);
199217
this.refCache.push(this.currentActivatedComp, pageRoute, outletMap, loaderRef);
200218
}

Diff for: tests/e2e-tests/lazy-load-routing.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ describe("lazy load routing", function () {
6161
.should.eventually.exist
6262
.text().should.eventually.equal("Second: lazy-load")
6363
.elementByAccessibilityId("router-location-strategy-states-lazy-load")
64-
.text().should.eventually.equal("/second/lazy-load")
64+
.text().should.eventually.equal("/first/lazy-load,/second/lazy-load")
6565
});
6666
});

0 commit comments

Comments
 (0)