Search code examples
javascriptangularjsoclazyload

Injecting into lazy loaded AngularJS Controller when using ocLazyLoad


I started using ocLazyload to lazy load few of my AngularJs controllers. I have used it along with the $routeProvider like this

  $routeProvider.when("/login", {
        templateUrl: "login.html",
        resolve: {
            loadCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load('LoginCtrl.js');
            }]
        }
    }).

and this works fine.

I have another route definition which uses resolve property to resolve few items before loading the controller.

 when("/dashboard", {
        templateUrl: "dashboard.html",
        controller: "DashboardCtrl",
        resolve: {
            profileData: getProfile,
            count : getCount
        }
    }).

Now I want to lazy load this controller too, and I tried it like this

   when("/dashboard", {
        templateUrl: "dashboard.html",
        resolve: {
            profileData: getProfile,
            count : getCount,
            loadCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load(['DashboardCtrl.js']);
            }]
        }
    }).

The page loads in this case, but the profileData and count doesn't get injected into the controller. The controller definition is as given below.

var app = angular.module('gt');
app.controller('DashboardCtrl', ['$scope', '$rootScope', 'profileData', 'count',
    function($scope, $rootScope, profileData, count) {
...
}]);

On debugging, I realise that the getProfile and getCount method get's called, but it happens asynchronously and the controller also lazy loads without waiting for these methods. How do I inject and lazy load at the same time? Can I use promises to resolve this in any way?

I am using AngularJS 1.3.10 & ocLazyLoad 1.0.5 versions

getProfile function for reference

var getProfile = function($q, $http, Profile, localStorageService) {
        var deferred = $q.defer();
        if (!localStorageService.get("loggedInUser")) {
            $http.post('/loggedin').success(function(user) {
                if (user) {
                    localStorageService.set("loggedInUser", user.email)
                    Profile.get(localStorageService.get("loggedInUser"), function(profileData) {
                        if (profileData) {
                            deferred.resolve(profileData);
                        } else {
                            deferred.resolve();
                        }
                    });
                } else {
                    deferred.reject();
                }
            });
        } else {
            Profile.get(localStorageService.get("loggedInUser"), function(profileData) {
                if (profileData) {
                    deferred.resolve(profileData);
                } else {
                    deferred.resolve();
                }
            });
        }
        return deferred.promise;
    }

getProfile.$inject = ["$q", "$http", "Profile", "localStorageService"];

Solution

  • I could get this working with the following configuration of $routeProvider

    when("/dashboard", {
        templateUrl: "dashboard.html",
        controller :"DashboardCtrl"
        resolve: {
            profileData: getProfile,
            count : getCount,
            loadCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
                return $ocLazyLoad.load(['DashboardCtrl.js']);
            }]
        }
    }).
    

    where DashboardCtrl is the controller defined in DashboardCtrl.js