Dhanush Gopinath
Dhanush Gopinath

Reputation: 5739

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"];

Upvotes: 0

Views: 1272

Answers (3)

Olivier
Olivier

Reputation: 1269

If you need your resolves to happen in a specific order, you can inject them into another resolve, like this:

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

Upvotes: 0

Dhanush Gopinath
Dhanush Gopinath

Reputation: 5739

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

Upvotes: 1

Sonaryr
Sonaryr

Reputation: 1122

does getProfile and getCount return a promise? I would guess this is the problem as this is required. Every object put in resolve should return a promise. see the documention

Upvotes: 0

Related Questions