ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

bug: $ionicView.leave doesn't fire with nested views.

Open yjukaku opened this issue 11 years ago • 39 comments

Type: bug

Platform: all

If you have an app with a nested ion-nav-view, the "$ionicView.leave" event is not called on the child view. (It is called on the parent view) You have to resort to using "$destroy".

In the CodePen below, you can see that the child controllers "$ionicView.leave" event is not called.

CodePen:

http://play.ionic.io/app/6eafd1a2beb4

yjukaku avatar Aug 11 '15 22:08 yjukaku

In my case $ionicView.beforeLeave is fired normally, but $ionicView.leave and $ionicView.afterLeave are not fired.

I tried to trace back the Ionic https://github.com/driftyco/ionic/blob/e2727d2e8f0815c3418e1fc29c92e2180e513408/js/angular/service/viewSwitcher.js code. This is all I got:

  1. The after step does not fire $ionicView.leave and $ionicView.afterLeave because leavingEle (https://github.com/driftyco/ionic/blob/e2727d2e8f0815c3418e1fc29c92e2180e513408/js/angular/service/viewSwitcher.js#L303) is undefined (so does the leavingScope variable)
  2. Said leavingEle is undefined because it never gets assign from https://github.com/driftyco/ionic/blob/e2727d2e8f0815c3418e1fc29c92e2180e513408/js/angular/service/viewSwitcher.js#L78
  3. Which because somehow navViewCtrl.getViewElements() returns less elements than usual and does not include the leavingEle but contain the enteringEle

However the before step is working just fine (and navViewCtrl.getViewElements() returns nav elements that include both the leavingEle and enteringEle).

I ran out of time and couldn't dig further, hope this information help you guys.

deoxen0n2 avatar Apr 15 '16 19:04 deoxen0n2

any progess on this issue? I need an event to cancel my timers... setting cache to false is not a solution (the view is very very heavy: gmaps + ++++ lots of things...)

thanks

clembo590 avatar Apr 19 '16 10:04 clembo590

@clembou590 Could you use $ionicView.beforeLeave to cancel your timers instead? It works on my case.

deoxen0n2 avatar Apr 19 '16 10:04 deoxen0n2

No it's not called.

    $scope.$on('$ionicView.leave', function() {
        console.log("leave main renter view");

    });


    $scope.$on('$ionicView.beforeLeave', function() {
        console.log("beforeLeave main renter view");

    });



    $scope.$on('$ionicView.afterLeave', function() {
        console.log("afterLeave main renter view");

    });

none of these are called when I leave the "abstract state" ex: when going from sideMenu.page1 to sideMenu.page2 everything is fired. but when going from sideMenue.page1 to "anotherPage" nothing is fired.

clembo590 avatar Apr 19 '16 20:04 clembo590

Hello everyone,

I have looked into this issue and it seems like a risky fix for Ionic 1. It would be a major undertaking. We're really sorry about that. Navigation and the life cycle events are complicated and somewhat fragile, so I am hesitant to make a change and break something else.

I have a work around that is low risk:

In your parent view controller, listen for the $ionicView.beforeLeave event. This event fires more reliably then the $ionicView.leave event when interacting with one-or-more abstract and parent views.

After your parent controller receives this event, you can $broadcast an event down to the child views/controllers.

$scope.$on("$ionicView.beforeLeave", function(event, data){
   // the parent has received the event, so broadcast an event to the children
   $scope.$broadcast("parentViewLeaving", {});
});

We know this is not ideal - but it will work.

Please let me know if you have any issues with this. If we come up with a low risk solution for this, we will re-open this issue.

I have tested this with @c2software's excellent plunkr. Thank you for contributing everyone.

Thanks, Dan

danbucholtz avatar Apr 22 '16 16:04 danbucholtz

Hello everyone,

We made some changes to life cycle events and the life cycle events should fire from the correct scope reliably in the next version of Ionic. We also added some new events that fire downwards specifically for the case of child/nested ui-router views.

Please let me know if you have any questions, comments, concerns, etc. Thanks again for contributing and for using Ionic.

https://github.com/driftyco/ionic/commit/f19b2fe9f99cb2b3cfb906f07e68f2fcaf3fbc6a

Thanks, Dan

danbucholtz avatar May 05 '16 13:05 danbucholtz

@danbucholtz So this issue will not be fixed until Ionic 2 release?

reinaldorauch avatar May 15 '16 00:05 reinaldorauch

No, it was released in 1.3.1 which went out into the wild on Thursday. See the release here.

danbucholtz avatar May 15 '16 00:05 danbucholtz

Yes it's working after 1.3.1

ghost avatar Jun 03 '16 15:06 ghost

If you cannot upgrade to 1.3.1 for some reason, $scope.$on('$destory', function () {...}) can be a alternative solution. Be careful with cache: false in router. Hope this can be helpful someone struggle with this problem :)

JeongNamHoon avatar Jul 13 '16 07:07 JeongNamHoon

I am using Ionic 1.3.1 and my $ionicView.leave still does not fire when switching tabs.

babinc avatar Sep 20 '16 14:09 babinc

@babinc,

Are your templates containers? If not, can you wrap in a div? This should resolve the issue.

Thanks, Dan

danbucholtz avatar Sep 20 '16 14:09 danbucholtz

my templates are using templateUrl's with an html file and an ion-view. I managed to get what i needed to work by using a combination of $destroy and $ionicView.beforeLeave.

babinc avatar Sep 20 '16 15:09 babinc

I was having similar issues and it turns out to be caused by what @babinc described. In my view's html file, I also had some of my modal templates. Example:

<script id="custom-modal.html" type="text/ng-template">
    <div class="modal">
        <ion-header-bar class="bar bar-stable">
            <!-- Amazing Header -->
        </ion-header-bar>
        <ion-content delegate-handle="createScroll">
            <!-- Amazing Content -->
        </ion-content>
    </div>
</script>

Moving these <script type="text/ng-template"> into their own html files alone made it so all of the events I used to use like $ionicView.leave and $ionicView.enter all started working again.

cc: @danbucholtz thanks for the help, it was indeed "something else"

gylippus avatar Sep 21 '16 06:09 gylippus

aah, beforeLeave fires if you wrap the abstract state ion-nav-view in a div element like this: template: '<div><ion-nav-view animation="slide-left-right" class="ion-nav-view-nested"/></div>', instead of template: '<ion-nav-view animation="slide-left-right" class="ion-nav-view-nested"/>',

then everything works

Freundschaft avatar Sep 27 '16 15:09 Freundschaft

I'm using Ionic v1.7.13 and i have same problema.

My app enter in first view, using tabs, but not fire any view life cycle (loaded, enter, beforeEnter...)

This problem persist when navigate by tabs.

ghost avatar Oct 13 '16 04:10 ghost

I found a work around for this. You can use this to observe any 'to and from' view changes. Just doing a string comparison on the url of the fromState emulated ionicView.leave well enough to achieve what I needed it for. Keep in mind you need to be using UI-router to do this. Hope this helps!

$rootScope.$on('$stateChangeStart', 
    function(event, toState, toParams, fromState, fromParams, options){ 
        if(fromState.url == "/video/:Id"){
            console.log("Leaving the view.");
        }
    });

austinemayer avatar Dec 19 '16 21:12 austinemayer

hi,everyone if you have modal content ,don't write it in main page,write it in new page instead. it will work

lafflan avatar Jan 13 '17 08:01 lafflan

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Sep 05 '18 03:09 ionitron-bot[bot]