angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

How can I get module name by the controller or the route

Open chriswu14 opened this issue 13 years ago • 1 comments

I want to limit access to some pages(modules) based on the route and user's right. So what I do is simply call $location.url("/403") when the condition is met. I came up with following ideas:

1, check the url string to see if the module keyword is in it (the route I defined reflects the module well) 2, give a property as the module name to the route definition then access it from $route.current.$$route.module to check the condition.

$routeProvider.when('/module1', { templateUrl: module.template('module.cshtml'), title: "Module1", module:"Module1" });

3, Somehow get the module name from the route.

Does someone know if there is a way to retrieve the module name from the route or the controller? Or I just did not understand the module concept correctly.

chriswu14 avatar Sep 20 '13 05:09 chriswu14

Angular modules are only really used for specifying what components (controllers, directives, services, etc) to add to the injector for the current application. They have no other effect, such as scoping or namespacing.

I would suggest that you add an extra property to each route that specifies the "module" as you do it (2) and then check that in $routeChangeStart.

$rootScope.$on('$routeChangeStart', function(event, current, previous) {
  if ( current.module ... ) {
  }
});

Alternatively you could do checks in a resolve, which rejects if the user is not authorized causing the route change to fail.

petebacondarwin avatar Sep 21 '13 18:09 petebacondarwin