使用require.js

时间:2015-12-10 17:49:50

标签: angularjs requirejs lazy-loading

我可以通过执行以下操作来延迟加载控制器,

第1步:添加其他配置...

rootModule.config([
    "$controllerProvider", function($controllerProvider) {
        rootModule.registerController = $controllerProvider.register;
    }
]);

Step2:根据步骤1中定义的registerController定义控制器

angular.module("rootModule").registerController("authController",
 function ($scope, $location, $rootScope, authService) {
     $scope.userName = "";
     $scope.userPwd = "";
     $scope.authenticate = function ()...

     $scope.testFunction = function ()...

 });

步骤3:在路由过程中加载控制器,

 rootModule
    .config([
        '$routeProvider',
        function ($routeProvider) {
            $routeProvider.when('/',
                {
                    templateUrl: 'templates/Login.html',
                    resolve: {
                        load: ["$q", function($q) {
                            var defered = $q.defer();
                            require(["Controllers/authController"], function() {
                                defered.resolve();
                            });
                            return defered.promise;
                        }]
                    }
                }).

现在,问题是我有一个名为&#34; authService&#34;的服务,我想懒加载,怎么做?这是服务... < / p>

define(function() {

angular.module("rootModule").service("authService", function ($http) {
    return {
    /////Something code here//////
   });
});

1 个答案:

答案 0 :(得分:1)

最后,由于Dan Wahlin写的this精彩博客,这非常简单。

要根据路由在运行时加载服务,我必须这样做......

第1步:在我的rootModule(包含路由信息的模块)配置中获取对$ provide.service()方法的引用...

rootModule.config(["$controllerProvider","$provide",
    "$controllerProvider", "$filterProvider","$compileProvider", function ($controllerProvider, $provide) {
        rootModule.registerController = $controllerProvider.register; //for controllers
        rootModule.registerService = $provide.service; //for services
        rootModule.registerFilter = $filterProvider.register; //for filters
        rootModule.registerDirective = $compileProvider.directive; //for directives
        rootModule.registerFactory = $provide.factory; //for factory
    }
]);

第2步:注册要动态加载的服务

define(function() {
angular.module("rootModule").registerService("reviewReportsService", function () {
    return {
              sampleData: "This is some sample data"
           }
       });
});

步骤3:解析服务脚本文件,在加载相应路径时加载

when('/ReviewAndSubmit',
                {
                    controller: "reviewAndSubmitController",
                    templateUrl: "templates/ReviewAndSubmit.html",

                    resolve: {
                        load: ["$q", function ($q) {
                            var defered = $q.defer();
                            require(["Controllers/reviewAndSubmitController"], function () {
                                defered.resolve();
                            });
                            require(["Services/reviewReportsService"], function () {
                                defered.resolve();
                            });
                            return defered.promise;
                        }]
                    }
                })

希望这有助于某人......