Ionic router: determining how a view has been entered

I am using Ionic v1.0.0-rc.2.

As controller + views are cached in Ionic, one has to take care to properly initialize the controller $scope.

I am using the following callback to do this:

$scope.$on('$ionicView.beforeEnter', function(){
     ...
}

However, to know how to initialize the $scope I need to know how the view was called:

  1. Was it called by a back button (A --> B --> back to A)
    • in this case I want to leave most items as is
  2. Was it returned to after pressing a back button (A --> B --> back to A --> return to B)
    • in this case I want to leave most items as is
  3. Was it entered following a new path (not back or forward) ... note that the the view may have already been instantiated from previous navigations
    • in this case I want to re-initialize the view

I have been looking at the documentation below, but could not find a way of doing this, or what parameters, if any, are passed into the callback function. Any documentation pointers would be great.

To determine what's going on in routes I chain this:

// Add state change hooks to log issues to console
.run(['$rootScope', '$state', '$urlMatcherFactory', function($rootScope, $state, $urlMatcherFactory) {

            $rootScope.$state = $state;

            function message(to, toP, from, fromP) { return from.name  + angular.toJson(fromP) + " -> " + to.name + angular.toJson(toP); }

            $rootScope.$on("$stateChangeStart", function(evt, to, toP, from, fromP) { console.log("Start:   " + message(to, toP, from, fromP)); });
            $rootScope.$on("$stateChangeSuccess", function(evt, to, toP, from, fromP) { console.log("Success: " + message(to, toP, from, fromP)); });
            $rootScope.$on("$stateChangeError", function(evt, to, toP, from, fromP, err) { console.log("Error:   " + message(to, toP, from, fromP), err); });
        }])

This should get you going in the right direction with some customization.