博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[AngularJS] Lazy loading Angular modules with ocLazyLoad
阅读量:4980 次
发布时间:2019-06-12

本文共 4277 字,大约阅读时间需要 14 分钟。

With the  you can load AngularJS modules on demand. This is very handy for runtime loading of Angular modules in large applications.

 Simple example:

angular.module("demo", ["oc.lazyLoad"])    .controller("AppCtrl", function ($injector, $ocLazyLoad) {        var app = this;        app.click = function () {            $ocLazyLoad.load({                name: "store",                files: [                    "store.js"                ]            }).then(function () {                console.log($injector.get("cart"));            })        }    })

 

 

'use strict';// Declare app level module which depends on filters, and servicesvar App = angular.module('app', ['ui.router', 'oc.lazyLoad'])  .config(function($stateProvider, $locationProvider, $urlRouterProvider, $ocLazyLoadProvider) {    $urlRouterProvider.otherwise("/");    $locationProvider.hashPrefix('!');    // You can also load via resolve    $stateProvider      .state('index', {        url: "/", // root route        views: {          "lazyLoadView": {            controller: 'AppCtrl', // This view will use AppCtrl loaded below in the resolve            templateUrl: 'partials/main.html'          }        },        resolve: { // Any property in resolve should return a promise and is executed before the view is loaded          loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {            // you can lazy load files for an existing module            return $ocLazyLoad.load({              name: 'app',              files: ['js/AppCtrl.js']            });          }]        }      })      .state('modal', {        parent: 'index',        resolve: { // Any property in resolve should return a promise and is executed before the view is loaded          loadOcModal: ['$ocLazyLoad', '$injector', '$rootScope', function($ocLazyLoad, $injector, $rootScope) {            // Load 'oc.modal' defined in the config of the provider $ocLazyLoadProvider            return $ocLazyLoad.load({              name: 'oc.modal',              files: [                'bower_components/bootstrap/dist/css/bootstrap.css', // will use the cached version if you already loaded bootstrap with the button                'bower_components/ocModal/dist/css/ocModal.animations.css',                'bower_components/ocModal/dist/css/ocModal.light.css',                'bower_components/ocModal/dist/ocModal.js',                'partials/modal.html'              ]            }).then(function() {              $rootScope.bootstrapLoaded = true;              // inject the lazy loaded service              var $ocModal = $injector.get("$ocModal");              $ocModal.open({                url: 'modal',                cls: 'fade-in'              });            });          }],          // resolve the sibling state and use the service lazy loaded          setModalBtn: ['loadOcModal', '$rootScope', '$ocModal', function(loadOcModal, $rootScope, $ocModal) {            $rootScope.openModal = function() {              $ocModal.open({                url: 'modal',                cls: 'flip-vertical'              });            }          }]        }      });    // Without server side support html5 must be disabled.    $locationProvider.html5Mode(false);    // We configure ocLazyLoad to use the lib script.js as the async loader    $ocLazyLoadProvider.config({      debug: true,      events: true,      modules: [{        name: 'gridModule',        files: [          'js/gridModule.js'        ]      }]    });  });

 

lazy loading is more for projects that are huge with many people working on it, and you have tons and tons of files. Instead of concatenating every JavaScript file into a few megabytes loaded up front, it would make more sense to lazy load them.

But if you're just working on a small project it makes more sense just to concatenate everything and serve up one giant JavaScript file up front. That way you don't have to worry about files failing to load later on and things like that.

It's really a call on your end you have to make as to whether you need to lazy load or not. Typically I'd say you don't need to and you don't have to but if you're working on a large project and it's a huge load up front then lazy loading is something you should look into.

 

Read More: 

转载于:https://www.cnblogs.com/Answer1215/p/4200263.html

你可能感兴趣的文章
[USACO 6.3.2] Cryptcowgraphy
查看>>
.net 开发人员面试题 - 多线程
查看>>
PHP实现的快速排序
查看>>
Dave Python 练习十七 -- 正则表达式
查看>>
混沌开窍---24幅由算法生成的正方形图像
查看>>
java中newInstance和new(转)
查看>>
面向对象技术
查看>>
关于网络模型中的同步异步的思考
查看>>
centos7 Linux 安装mysql
查看>>
dom的综合练习
查看>>
python中sort方法
查看>>
python字符串
查看>>
pb设计笔记
查看>>
ADO.NET中调用存储过程
查看>>
开发者 发展 2 码路指南
查看>>
830. Positions of Large Groups
查看>>
Bootstrap_网格系统
查看>>
java学习——异常处理
查看>>
实验10: RIP
查看>>
前端开发 - CSS - 总结
查看>>