0

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"];
1
  • That was an useful info. Commented Oct 14, 2015 at 7:02

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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']);
        }]
    }
}).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.