Ahmad.Masood Ahmad.Masood - 3 months ago 7
AngularJS Question

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

Answer

Try this:

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