[AngularJS] Lazy loading Angular modules with ocLazyLoad
With the ocLazyLoad 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 services
- var 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://github.com/ocombe/ocLazyLoad/blob/master/examples/complexExample/js/app.js
[AngularJS] Lazy loading Angular modules with ocLazyLoad的更多相关文章
- [AngularJS] Lazy Loading modules with ui-router and ocLazyLoad
We've looked at lazy loading with ocLazyLoad previously, but what if we are using ui-router and want ...
- [Angular Router] Lazy loading Module with Auxiliary router
Found the way to handle Auxiliary router for lazy loading moudle and erge load module are different. ...
- [Angular2 Router] Lazy Load Angular 2 Modules with the Router
Angular 2 lazy loading is a core feature of Angular 2. Lazy loading allows your application to start ...
- Angular2+typescript+webpack2(支持aot, tree shaking, lazy loading)
概述 Angular2官方推荐的应该是使用systemjs加载, 但是当我使用到它的tree shaking的时候,发现如果使用systemjs+rollup,只能打包成一个文件,然后lazy loa ...
- AngularJS: Dynamically loading directives
http://www.codelord.net/2015/05/19/angularjs-dynamically-loading-directives/ ----------------------- ...
- Lazyr.js – 延迟加载图片(Lazy Loading)
Lazyr.js 是一个小的.快速的.现代的.相互间无依赖的图片延迟加载库.通过延迟加载图片,让图片出现在(或接近))视窗才加载来提高页面打开速度.这个库通过保持最少选项并最大化速度. 在线演示 ...
- Can you explain Lazy Loading?
Introduction Lazy loading is a concept where we delay the loading of the object until the point wher ...
- [AngularJS] Sane, scalable Angular apps are tricky, but not impossible.
Read fromhttps://medium.com/@bluepnume/sane-scalable-angular-apps-are-tricky-but-not-impossible-less ...
- iOS swift lazy loading
Why bother lazy loading and purging pages, you ask? Well, in this example, it won't matter too much ...
随机推荐
- Winform使用DevExpress的WaitDialogForm画面 z
使用了DevExpress的WaitDialogForm 在应用程序加载开始时新建一个线程,并将loading画面show起来,在应用程序画面弹出前将该线程终止. 代码: private DevExp ...
- longest common str
#include <vector> #include <iostream> #include <string> using namespace std; int l ...
- HNU 13108-Just Another Knapsack Problem (ac自动机上的dp)
题意: 给你一个母串,多个模式串及其价值,求用模式串拼接成母串(不重叠不遗漏),能获得的最大价值. 分析: ac自动机中,在字典树上查找时,用dp,dp[i]拼成母串以i为结尾的子串,获得的最大价值, ...
- HDU-1584 蜘蛛牌(dfs)
可以多看看. 蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 精妙SQL语句 基础
精妙SQL语句SQL语句先前写的时候,很容易把一些特殊的用法忘记,我特此整理了一下SQL语句操作,方便自己写SQL时方便一点,想贴上来,一起看看,同时希望大家能共同多多提意见,也给我留一些更好的佳句, ...
- python学习之dict的items(),values(),keys()
Python的字典的items(), keys(), values()都返回一个list >>> dict = { 1 : 2, 'a' : 'b', 'hello' : 'worl ...
- 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!
UVa11995 I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...
- web服务器分析与设计(四)
上篇已经开始了系统内部的分析,并且得到一些分析对象.在整个动作场景中,我们得到了一些粗略的对象.有必要对对象进行分析,合并,再抽象. 实质是职责的合理分配,使得系统合乎功能性,同时得到最大的可扩展,可 ...
- ArrayList、LinkedList、HashMap的遍历及遍历过程中增、删元素
ArrayList.LinkedList.HashMap是Java中常用到的几种集合类型,遍历它们是时常遇到的情况.当然还有一些变态的时候,那就是在遍历的过程中动态增加或者删除其中的元素. 下面的例子 ...
- Sqoop 命令
1)list-databases List available databases on a server sqoop list-databases --connect jdbc:db2:// ...