[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 ...
随机推荐
- IT版孔乙己(转)
[不要做学究]回忆孔先生IT版 我关于本文评价:看到这篇文章很多人会生出这样的疑问“这明明是在诋毁钻研技术的人嘛?是不是在宣扬技术无用论?”. 初看这篇文章的时候我也是这样的想法,但是逐步才明白这篇文 ...
- linux 命令——文件管理 cat
一.介绍 cat 是一个文本文件查看和连接工具.从第一个字节开始正向查看文件的内容. 主要有三大功能: 1.一次显示整个文件.$ cat filename ~,y6;e2.从键盘创建一个文件.$ ...
- 【LeetCode 208】Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 微信web调试工具
做微信公众号开发的时候,最头疼的一件事,莫过于bug调试了. 由于有些功能涉及到权限问题,必须在微信公众号内打开,所以对开发人员来说就是遇到了一场噩梦. 这里有两种方法: 1.工具:mac.使用微信m ...
- libyuv颜色空间转换开源库
libyuv据说在缩放和颜色空间转换,比ffmpeg效率高很多倍.不知道和我们的PP库比起来怎么样.同样有neon指令集优化.支持移动设备.
- 我从其他人的Shell脚本中学到的
我从其他人的Shell脚本中学到的 2013/08/20 | 分类: 程序员 | 2 条评论 | 标签: SHELL, 脚本 分享到:17 本文由 伯乐在线 - 伯乐在线读者 翻译自 Fizer Kh ...
- poj 3026 Borg Maze (BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit:1000MS Memory Limit:65536KB 64bit IO For ...
- HDU 1455 http://acm.hdu.edu.cn/showproblem.php?pid=1455
#include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #de ...
- HDU 1828 Picture (线段树+扫描线)(周长并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...
- dll开发中遇到的问题
刚碰到个问题,我的一个项目中引用了一个dll,这个dll又引用了另一个dll,我把这俩个都放在bin文件夹下,但是会报错,说第二个dll找不到.把它放到系统文件夹system32下就没事了. 但是遇到 ...