Here I will explain how to use single Route to load partial views dynamically based on Menu links.
The implementation idea for dynamic routing came from:
- My previous Blog post: https://rajudasa.blogspot.in/2016/04/dynamically-loading-angularjs.html
- and some research.
We know that client side code is accessible to end users.
Our application is a SPA, created using AngularJS and it has roles based authorization.
You don't want to show all AngularJS Route mappings defined in your application to all role users, since a low role user/hacker (customer) may try to access views defined for high role users (Admin).
Eventhough we may have setup authorization security for operations at server side, hiding Route mappings at client side provides a first level of security.
Our application renders specific Menu links based on authenticated user role, it has only single route mapping (wildcard) to handle all partial view links with dynamic controller. This controller handles registering new route, loading template and controller dynamically based on Menu link clicked.
Since controllers are also loaded dynamically, other role users can't access the controller logic intended for particular role.
To make this approach work, our Menu links point to the single wildcard route which is then converted to actual route by the dynamic controller.
Single Rout Map:
From above code, you can see all link paths start with "/app/viewName" which are then converted to "/viewName".
Note that viewName is also used as file name for template and controller files, if you want you can modify the code to follow your naming convention for those files.
$routeProvider is leaked into global scope.
For full code, download from here.