Ionic - View Leave Events Never Called

后端 未结 4 587
旧巷少年郎
旧巷少年郎 2021-01-19 00:41

After updating an existing Ionic project from 1.13-beta to 1.14-beta I\'ve experienced some behaviour I can\'t explain. When changing from one viewstate to another, the old

相关标签:
4条回答
  • 2021-01-19 00:53

    If your view is wrapped in a tab using <ion-tab>, you need to register for $ionicNavView-Events:

     $scope.$on("$ionicNavView.leave")
    
    0 讨论(0)
  • 2021-01-19 00:57

    I had this problem the other day. I was listening for the event on my child controllers. When I moved the listener to the parent, it worked. I wasn't quite sure why, but now I think I know why. It's because of the way Ionic caches the views. Specifically this line from the docs:

    If a view leaves but is cached, then this event will not fire again on a subsequent viewing.

    It's likely that your views were cached before you added the listener and the leave event never fired. But since your parent is... well... the parent, its leave event is triggered any time you leave any of its children. I haven't officially tested this, it's just a hunch. To test, try telling ionic to not cache the view by adding cache-view="false" to your ion-view declaration.

    See docs here.

    0 讨论(0)
  • 2021-01-19 01:00

    Found the problem. The 'Main' state was defined without the attribute

    abstract: true
    

    this worked in Ionic 1.13-beta but Aparently breaks in Ionic 1.14-beta. Didn't find this change in any of the migration posts on either Ionic, AngularJS or Angular-ui-router, so I don't know why this solved it. If anyone can elaborate more on the behavior in this situation, I'd be grateful but for now I have a solution.

    0 讨论(0)
  • 2021-01-19 01:11

    This is an open issue https://github.com/driftyco/ionic/issues/2869 and probably occurs when view-cache="false".

    On my case $ionicView.leave worked in nested views but not when moving between tabs nor did $ionicParentView.leave so I came around it with another solution.

    Solution: On MAIN controller I added:

    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
         if (fromState.name === 'name-of-leaving-state') {
    
          //your code here, similar behavior with .$on('$ionicView.leave') 
    
         }
    });
    

    Hope it helps

    0 讨论(0)
提交回复
热议问题