Skip to content
This repository was archived by the owner on May 4, 2022. It is now read-only.
This repository was archived by the owner on May 4, 2022. It is now read-only.

$ionicView.beforeLeave for other $scope is called #77

Open
@jgw96

Description

@jgw96

From @originrobot on October 25, 2016 4:9

ENV:
Cordova CLI: 6.3.1
Ionic Framework Version: 1.3.1
Ionic CLI Version: 1.7.16
Ionic App Lib Version: 0.7.3

I have a directive in which I register $ionicView.beforeLeave like below:

var link = function (scope, elem, attr) {
    var $scope = scope;
    $scope.$on('$ionicView.beforeLeave', function (event, viewData) {
      // do something here
    });
  }

And when we enter the same page with different params, we found that every time when $ionicView.beforeLeave is called, its $scope is the the one that the first time we enter the page. Thus we can't access parameters for the current $scope.

The reason for this is in function getScopeForElement, the code like below:

var elements = aggregateNavViewChildren(element);
      for ( var i = 0; i < elements.length; i++ ) {
          var state = angular.element(elements[i]).attr("state");
          if ( state === stateData.stateName ) {
            stateData.abstractView = true;
            return angular.element(elements[i]).scope();
          }
      }

We can see that it only check the stateName but actually the state is 'cached' not 'active'. Thus always the first $scope is returned.

I try to change it to:

var elements = aggregateNavViewChildren(element);
      for ( var i = 0; i < elements.length; i++ ) {
          var state = angular.element(elements[i]).attr("state");
        var navView = angular.element(elements[i]).attr("nav-view");
          if ( state === stateData.stateName && navView !== 'cached' ) {
            stateData.abstractView = true;
            return angular.element(elements[i]).scope();
          }
      }

It then works but I'm not sure if the change has any side effect. Can anyone in ionic team confirm it?

Copied from original issue: ionic-team/ionic-framework#8889

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions