@@ -275,9 +275,13 @@ export class NSLocationStrategy extends LocationStrategy {
275275
276276 if ( ! outlet ) {
277277 const topmostFrame = this . frameService . getFrame ( ) ;
278- this . currentOutlet = this . getOutletByFrame ( topmostFrame ) ;
278+ this . currentOutlet = this . getOutletByFrame ( topmostFrame ) || this . currentOutlet ;
279+ }
280+
281+ const frameToBack : Frame = this . currentOutlet . getFrameToBack ( ) ;
282+ if ( frameToBack ) {
283+ frameToBack . goBack ( ) ;
279284 }
280- this . currentOutlet . getFrameToBack ( ) . goBack ( ) ;
281285 } else {
282286 // Nested navigation - just pop the state
283287 if ( isLogEnabled ( ) ) {
@@ -385,10 +389,11 @@ export class NSLocationStrategy extends LocationStrategy {
385389 routerLog ( "NSLocationStrategy._beginModalNavigation()" ) ;
386390 }
387391
388- this . currentOutlet = this . getOutletByFrame ( frame ) ;
392+ this . currentOutlet = this . getOutletByFrame ( frame ) || this . currentOutlet ;
389393
390394 // It is possible to have frame, but not corresponding Outlet, if
391- // showing modal dialog on app.component.ts ngOnInit() e.g.
395+ // showing modal dialog on app.component.ts ngOnInit() e.g. In that case
396+ // the modal is treated as none modal navigation.
392397 if ( this . currentOutlet ) {
393398 this . currentOutlet . showingModal = true ;
394399 this . _modalNavigationDepth ++ ;
@@ -400,15 +405,18 @@ export class NSLocationStrategy extends LocationStrategy {
400405 routerLog ( "NSLocationStrategy.closeModalNavigation()" ) ;
401406 }
402407
403- if ( this . _modalNavigationDepth > 0 ) {
408+ const isShowingModal = this . _modalNavigationDepth > 0 ;
409+
410+ if ( isShowingModal ) {
404411 this . _modalNavigationDepth -- ;
405412 }
406413
407414 // currentOutlet should be the one that corresponds to the topmost() frame
408415 const topmostOutlet = this . getOutletByFrame ( this . frameService . getFrame ( ) ) ;
409- this . currentOutlet = this . findOutletByModal ( this . _modalNavigationDepth , true ) || topmostOutlet ;
416+ const outlet = this . findOutletByModal ( this . _modalNavigationDepth , isShowingModal ) || topmostOutlet ;
410417
411- if ( this . currentOutlet ) {
418+ if ( outlet ) {
419+ this . currentOutlet = outlet ;
412420 this . currentOutlet . showingModal = false ;
413421 this . callPopState ( this . currentOutlet . peekState ( ) , false ) ;
414422 }
@@ -481,7 +489,8 @@ export class NSLocationStrategy extends LocationStrategy {
481489 this . callPopState ( null , true , currentOutlet ) ;
482490 }
483491
484- if ( ! currentOutlet . isNSEmptyOutlet ) {
492+ // Skip frames filtering since currentOutlet is <router-outlet> when no frames available.
493+ if ( currentOutlet . frames . length && ! currentOutlet . isNSEmptyOutlet ) {
485494 currentOutlet . frames = currentOutlet . frames . filter ( currentFrame => currentFrame !== frame ) ;
486495 return currentOutlet . frames . length ;
487496 }
@@ -554,26 +563,32 @@ export class NSLocationStrategy extends LocationStrategy {
554563 return pathToOutlet || lastPath ;
555564 }
556565
557- findOutletByModal ( modalNavigation : number , isShowingModal ?: boolean ) : Outlet {
558- return this . outlets . find ( ( outlet ) => {
559- let isEqual = outlet . modalNavigationDepth === modalNavigation ;
560- return isShowingModal ? isEqual && outlet . showingModal : isEqual ;
561- } ) ;
562- }
563-
564566 findOutlet ( outletKey : string , activatedRouteSnapshot ?: ActivatedRouteSnapshot ) : Outlet {
565- let outlet : Outlet = this . outlets . find ( ( currentOutlet ) => currentOutlet . outletKeys . indexOf ( outletKey ) > - 1 ) ;
567+ let outlet : Outlet = this . outlets . find ( ( currentOutlet ) => {
568+ let equalModalDepth = currentOutlet . modalNavigationDepth === this . _modalNavigationDepth ;
569+ return equalModalDepth && currentOutlet . outletKeys . indexOf ( outletKey ) > - 1 ;
570+ } ) ;
566571
567572 // No Outlet with the given outletKey could happen when using nested unnamed p-r-o
568573 // primary -> primary -> prymary
569574 if ( ! outlet && activatedRouteSnapshot ) {
570575 const pathByOutlets = this . getPathByOutlets ( activatedRouteSnapshot ) ;
571- outlet = this . outlets . find ( ( currentOutlet ) => currentOutlet . pathByOutlets === pathByOutlets ) ;
576+ outlet = this . outlets . find ( ( currentOutlet ) => {
577+ let equalModalDepth = currentOutlet . modalNavigationDepth === this . _modalNavigationDepth ;
578+ return equalModalDepth && currentOutlet . pathByOutlets === pathByOutlets ;
579+ } ) ;
572580 }
573581
574582 return outlet ;
575583 }
576584
585+ private findOutletByModal ( modalNavigation : number , isShowingModal ?: boolean ) : Outlet {
586+ return this . outlets . find ( ( outlet ) => {
587+ let equalModalDepth = outlet . modalNavigationDepth === modalNavigation ;
588+ return isShowingModal ? equalModalDepth && outlet . showingModal : equalModalDepth ;
589+ } ) ;
590+ }
591+
577592 private getOutletByFrame ( frame : Frame ) : Outlet {
578593 let outlet ;
579594
0 commit comments