I am trying to define a generic route which will handle most of request. Adding a route for each and every feature addition is what I feel not good and not maintainable.
So my target is to write a generic route and resolve the decencies dynamically, which include resolving all dependent controller & service files, resolving templateUrl and resolving controller name. I can resolve/construct everything except controller name. Please help me a way around
My route with a custom resolver:
$routeProvider
.when('/:module/:controller/:action?', routeResolver.resolve())
Inside my custom resolver:
function routeResolverProvider(){
this.$get= function(){
return this;
}
this.resolve = function (options) {
var route = {};
route.templateUrl = function (params) {
var path = String.format('/app/components/{0}/{1}.view.html', params.module, params.controller);
return path;
};
//================================================================================
route.controller='THIS IS WHAT I WANT TO CONSTRUCT FROM ROUTE as templateUrl'
//================================================================================
route.resolve = {
loadDependencies: ['$q', '$rootScope', '$route', function ($q, $rootScope, $route) {
// MY RESOLVE LOGIC HERE
// I construct dependent file path from route values
// String.format('/app/components/{0}/{1}.controller.js', params.module, params.controller);
}]
};
return route;
}}
app.provider('routeResolver', routeResolverProvider)
Some good articles: https://github.com/angular/angular.js/wiki/Understanding-Dependency-Injection https://medium.com/opinionated-angularjs/advanced-routing-and-resolves-a2fcbf874a1c