Ahmad.Masood
Ahmad.Masood

Reputation: 1309

Angularjs: Call a function when ever a certain type of route is invoked

Following is my sample route config.

.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/admin', {
        templateUrl: 'admin/admin.html',
        controller: 'AdminCtrl'
    }).when('/admin/confrence',{
        templateUrl: 'admin/adminconfrence.html',
        controller: 'AdminCtrl'
    }).when('/admin/confrence/new',{
        templateUrl: 'admin/adminconfrencenew.html',
        controller: 'AdminCtrl'
    });
}])

I want to do some authentication check when ever any route is called which is beginning /admin. Can someone please guide me how it's possible

Upvotes: 0

Views: 44

Answers (1)

Jahongir Rahmonov
Jahongir Rahmonov

Reputation: 13763

Try this:

app.run(function($rootScope) {       
    $rootScope.$on('$routeChangeStart', function(event, next, current) {
        if (next.$$route.originalPath.indexOf("/admin")!= -1){
           // do your stuff
        }
    });
})

Upvotes: 3

Related Questions