0

In my MVC project i am using angularjs to produce single page application. every views produced by server load into ng-view by angular and maybe they have some ng-controller inside. so before they can be load i have handled and have deferred the view resolution, and then register some controller dynamically at run-time. the blow code describes this scenario

on the top of my page head i have this:

app.config(function ($controllerProvider) {
                // i hold app.controllerProvider to call later.
                app.controllerProvider = $controllerProvider;
    }

and

var routes={'URL1' , 'URL2' , 'URL3' , ... }
    app.config(function ($routeProvider) {
         routes.forEach(function (route) {
                    $routeProvider.when(route, {
                        templateUrl: route
                        ,
                        resolve: {
                            deps: PageResolver
                        }
                    });
                });
     }

and the resolver:

function PageResolver($q, $rootScope) {
   var deferred = $q.defer();


   app.controllerProvider.register('MyCTRL', [
     '$scope',
     function ($scope) {
       // ... some codes
   }]);


   deferred.resolve();
   return deferred.promise;
    }

these codes run and works fine ,but my problem is that the app.controllerProvider.register does not effect always! it means that i can't register controller dynamically?! or what is wrong?

Ali Adlavaran
  • 3,697
  • 2
  • 23
  • 47
  • Look at http://stackoverflow.com/questions/15250644/loading-an-angularjs-controller-dynamically – YD1m Apr 21 '15 at 09:59
  • it does not solve my problem, it gives me error ! [Error: [ng:areq] http://errors.angularjs.org/1.2.26/ng/areq?p0=CTRL&p1=not%20a%20function%2C%20got%20undefined] – Ali Adlavaran Apr 21 '15 at 10:26
  • Possible duplicate of [AngularJS: lazy loading controllers and content](http://stackoverflow.com/questions/25168593/angularjs-lazy-loading-controllers-and-content) – Paul Sweatte Nov 28 '15 at 23:00

0 Answers0