使用ocLazyLoad

时间:2015-09-24 06:39:29

标签: javascript angularjs oclazyload

我开始使用ocLazyload来延迟加载我的几个AngularJs控制器。我已将它与$routeProvider一样用于此

  $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方法,但它是异步发生的,并且控制器也在延迟加载而不等待这些方法。我如何同时注入和延迟加载?我可以使用promises以任何方式解决这个问题吗?

我正在使用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"];

3 个答案:

答案 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

中定义的控制器

答案 1 :(得分:0)

getProfilegetCount会回复承诺吗?我猜这是问题,因为这是必需的。放在resolve中的每个对象都应该返回一个承诺。见the documention

答案 2 :(得分:0)

如果您需要以特定顺序进行解决,您可以将它们注入另一个解决方案,如下所示:

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