2015-09-24 52 views
0

我开始使用ocLazyload来延迟加载我的AngularJs控制器中的几个。我用它一起$routeProvider像这样当使用ocLazyLoad时注入延迟加载的AngularJS控制器

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

和这工作正常。

我有另一个路由定义,它使用resolve属性来加载控制器之前解决一些项目。

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

现在我想延迟加载该控制器也和我想它像在这种情况下,这个

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

该页面加载,但profileDatacount不会被注入到控制器。控制器的定义如下。

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

在调试时,我意识到getProfilegetCount GET方法的调用,但它是异步进行的,并且控制器也懒的负载,而无需等待这些方法。如何在同一时间注入和延迟加载?我可以使用承诺以任何方式解决此问题吗?

我使用AngularJS 1.3.10 & ocLazyLoad 1.0.5版本

getProfile功能参考

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

这是一个有用的信息。 –

回答

1

我能得到与$routeProvider

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

以下配置,其中DashboardCtrlDashboardCtrl.js

0

确实getProfilegetCount返回一个承诺?我想这是问题,因为这是必需的。放入resolve的每个对象都应该返回一个承诺。看到the documention

+0

是的。添加getProfile方法供您参考 –

0

如果您需要解决了一个特定的顺序发生定义的控制这方面的工作,你可以将它们注入另一个解析中,如下所示:

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