Load dynamical controller with AngularAMD in route config

751 Views Asked by At

i have next route configuration:

    .config(["$routeProvider", function ($routeProvider) {
        $routeProvider
            .when('/something/:id', angularAMD.route({
                templateUrl  : function ($routeParams) {
                    return "views/something/" + $routeParams.id + ".html";
                },
                controllerUrl: function ($routeParams) {
                    return "controllers/something/" + $routeParams.id + "/main";
                }
            }))

Seems like angularAMD doesn't support controllerUrl as function and as result i can't define controllers dynamical.

The question is how i can load different controllers for different urls, mb i missed something or there is another workaround?

1

There are 1 best solutions below

0
On

You could try something like the bellow. It was taken from this Website(http://www.codeproject.com/Articles/808213/Developing-a-Large-Scale-Application-with-a-Single)

 $routeProvider.when("/something/:id", angularAMD.route({
                templateUrl: function (rp) { return 'views/' + rp.section + '/' + rp.tree + '.html'; },

                resolve: {

                    load: ['$q', '$rootScope', '$location', function ($q, $rootScope, $location) {

                        var path = $location.path();
                        var parsePath = path.split("/");
                        var parentPath = parsePath[1];
                        var controllerName = parsePath[2];

                        var loadController = "Views/" + parentPath + "/" + controllerName + "Controller";

                        var deferred = $q.defer();
                        require([loadController], function () {
                            $rootScope.$apply(function () {
                                deferred.resolve();
                            });
                        });
                        return deferred.promise;
                    }]
                }

            }))