title: Injecting into lazy loaded AngularJS Controller when using ocLazyLoad
body: <p>I started using ocLazyload to lazy load few of my AngularJs controllers. I have used it along with the <code>$routeProvider</code> like this </p>
  
  <pre><code>  $routeProvider.when("/login", {
          templateUrl: "login.html",
          resolve: {
              loadCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
                  return $ocLazyLoad.load('LoginCtrl.js');
              }]
          }
      }).
  </code></pre>
  
  <p>and this works fine.</p>
  
  <p>I have another route definition which uses <code>resolve</code> property to resolve few items before loading the controller.</p>
  
  <pre><code> when("/dashboard", {
          templateUrl: "dashboard.html",
          controller: "DashboardCtrl",
          resolve: {
              profileData: getProfile,
              count : getCount
          }
      }).
  </code></pre>
  
  <p>Now I want to lazy load this controller too, and I tried it like this </p>
  
  <pre><code>   when("/dashboard", {
          templateUrl: "dashboard.html",
          resolve: {
              profileData: getProfile,
              count : getCount,
              loadCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
                  return $ocLazyLoad.load(['DashboardCtrl.js']);
              }]
          }
      }).
  </code></pre>
  
  <p>The page loads in this case, but the <code>profileData</code> and <code>count</code> doesn't get injected into the controller. The controller definition is as given below.</p>
  
  <pre><code>var app = angular.module('gt');
  app.controller('DashboardCtrl', ['$scope', '$rootScope', 'profileData', 'count',
      function($scope, $rootScope, profileData, count) {
  ...
  }]);
  </code></pre>
  
  <p>On debugging, I realise that the <code>getProfile</code> and <code>getCount</code> 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?</p>
  
  <p>I am using AngularJS 1.3.10 &amp; ocLazyLoad 1.0.5 versions</p>
  
  <p><code>getProfile</code> function for reference </p>
  
  <pre><code>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"];
  </code></pre>