Darko Romanov
Darko Romanov

Reputation: 2805

Reset a $rootScope property on location change in Angular.js

in my application I have a $rootScope property called $rootScope.currentStatus used to set a css class on body under some circumstances.

The problem is that I need to reset the value to a default any time the user leaves the current page, but I don't know where I can intercept the location change. Currently I use a $routeProvider:

app.config(function ($routeProvider, $locationProvider, $httpProvider) {   
    $routeProvider.when('/site/dashboard', {
        templateUrl: '/partials/site/dashboard.html',
        controller: 'DashboardCntl',
        resolve: {
            loggedin: checkLoggedin
        }
    });

    ....
});

with many entries. I could use resolve adding a function that reset my variable but I'd have to insert in on all my routing entries.

Do you have any other idea?

Upvotes: 1

Views: 2143

Answers (1)

Darko Romanov
Darko Romanov

Reputation: 2805

Ok, I found the solution by myself, I can intercept the location change (route change) using this code:

$scope.$on('$routeChangeSuccess', function (next, current) {

    $rootScope.currentStatus = "";

});

so I don't have to reset the variable in all of my routing entries, I always reset it when route changes and then I set the proper value inside my controller.

Upvotes: 1

Related Questions