使用ocLazyLoad时注入到延迟加载的AngularJS Controller中

达努什·戈皮纳斯

我开始使用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) {
...
}]);

在调试时,我意识到getProfileandgetCount方法将被调用,但是它异步发生,并且控制器也延迟加载而无需等待这些方法。如何同时注入和延迟加载?我可以使用诺言以任何方式解决此问题吗?

我正在使用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"];
达努什·戈皮纳斯

我可以使用以下配置来工作 $routeProvider

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

DashboardCtrl控制器在哪里定义DashboardCtrl.js

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在SpringBoot Rest Controller中与Jackson @JsonIgnore合作进行MongoDB延迟加载

避免使用Swift在Xcode中从一个View Controller延迟到另一个View Controller

模板中的AngularJS“ Controller as”

嵌入到Navigation Controller中时,CollectionView不起作用

View Controller中的依赖注入

使用延迟加载时,如何在Angular中动态构建导航?

路由未定向到Laravel中的Controller

在Angular 6中使用延迟加载时,路由不适用于其他路由

在angularjs中动态加载CSS(加载延迟)

Symfony 2依赖注入到Controller构造中

无法使用Ninject将依赖项注入到从Angular Service调用的ASP.NET Web API Controller中

CakePHP Controller继承中的“使用”-变量

使用“ controller as”语法时,如何访问控制器中的“ this”?

如何使用Controller在Magento中创建产品?

无法在AngularJS中为局部视图注入Controller

使用Websockets时无法在Spring Controller中获取会话

AngularJS注入Factory到Controller奇怪的错误

在Controller中调用模态函数-AngularJS

在AngularJS中的ng-repeat中访问Controller变量

在Controller AngularJS中创建函数

如何将AngularJS自定义指令注入到引导程序预加载的模态中?

在Angular中使用“ Controller as”模式时回调中的数据

使用mail()时Controller中的Laravel错误500

如何在Laravel中调用端点时模拟注入Controller中的对象?

延迟加载和$ ocLazyLoad

在示例中的Controller中共享数据-AngularJs

外部文件中的AngularJs Controller和使用路由

在 Docker 中启动 Controller 时遇到的问题

将列表中的项目循环到 Controller 中的 ViewBag 中