AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖
好了,现进入正题,在 AngularJs 实现动态(懒)加载主要是依赖于3个主JS文件和一段依赖的脚本。
实现的过程主要是引用3个主要的JS文件
- <script src="angular/1.4.8/angular/angular.min.js"></script>
- <script src="angular/ui-router/release/angular-ui-router.min.js"></script>
- <script src="angular/oclazyload/src/ocLazyLoad.min.js"></script>
然后通过 APP 配置,将依赖的脚本进行注入操作
- var app = angular.module('pkcms', ["ui.router", "oc.lazyLoad"]);
- app.config(["$provide", "$compileProvider", "$controllerProvider", "$filterProvider",
- function ($provide, $compileProvider, $controllerProvider, $filterProvider) {
- app.controller = $controllerProvider.register;
- app.directive = $compileProvider.directive;
- app.filter = $filterProvider.register;
- app.factory = $provide.factory;
- app.service = $provide.service;
- app.constant = $provide.constant;
- }]);
- // 按模块化加载其他的脚本文件
- app.constant('Modules_Config', [
- {
- name: 'treeControl',
- serie: true,
- files: [
- "Scripts/angular-bootstrap/ui-bootstrap-tpls-0.14.3.min.js"
- ]
- }
- ]);
- app.config(["$ocLazyLoadProvider","Modules_Config",routeFn]);
- function routeFn($ocLazyLoadProvider,Modules_Config){
- $ocLazyLoadProvider.config({
- debug:false,
- events:false,
- modules:Modules_Config
- });
- };
以上是初始化动态加载的配置过程。
接着是建立路由
- "use strict"
- app.config(["$stateProvider","$urlRouterProvider",routeFn]);
- function routeFn($stateProvider,$urlRouterProvider){
- $urlRouterProvider.otherwise("/main");
- $stateProvider
- .state("main",{
- url:"/main",
- templateUrl:"views/main.html",
- controller:"mainCtrl",
- controllerAs:"main",
- resolve:{
- deps:["$ocLazyLoad",function($ocLazyLoad){
- return $ocLazyLoad.load("controllers/main.js");
- }]
- }
- })
- .state("adminUser",{
- url:"/adminUser",
- templateUrl:"views/adminUser.html",
- controller:"adminUserCtrl",
- controllerAs:"adminUser",
- resolve:{
- deps:["$ocLazyLoad",function($ocLazyLoad){
- return $ocLazyLoad.load("controllers/adminUser.js");
- }]
- }
- })
- };
最后是按路由配置的在对应目录下建2个HTML页面文件和2个JS文件用做测试
main.html
- <div>
- {{main.value}}
- </div>
adminUser.html
- <div>
- {{adminUser.value}}
- </div>
main.js
- /**
- * mainCtrl
- * Created by pkcms.cn on 2016/6/24.
- */
- (function () {
- "use strict"
- app.controller("mainCtrl", mainCtrlFn);
- function mainCtrlFn() {
- this.value = "Hello World";
- }
- }())
adminUser.js
- /**
- * adminUserCtrlFn
- * Created by pkcms.cn on 2016/6/24.
- */
- (function () {
- app.controller('adminUserCtrl',adminUserCtrlFn);
- function adminUserCtrlFn() {
- this.value = "welcome to admin user";
- }
- }());
github url :https://github.com/366065186/angularjs-oclazyload
转载请注明:PKCMS博客 » AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖
AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖的更多相关文章
- AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖-转
http://blog.csdn.net/zhangh8627/article/details/51752872 AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖 标签: ...
- AngularJs 动态加载模块和依赖
最近项目比较忙额,白天要上班,晚上回来还需要做Angular知识点的ppt给同事,毕竟年底要辞职了,项目的后续开发还是需要有人接手的,所以就占用了晚上学习的时间.本来一直不打算写这些第三方插件的学习笔 ...
- Angular ui-roter 和AngularJS 通过 ocLazyLoad 实现动态(懒)加载模块和依赖
什么是ui-router ui-router是AngularUI库最有用的组件之一(AngularUI库由AngularJS社区构建).它是一个第三方路由框架,允许通过状态机制组织接口,而不是简单的U ...
- angular5给懒加载模块添加loading
在根组件的构造函数中直接执行: this.router.events.subscribe(event=>{ if(event instanceof RouteConfigLoadEnd) { t ...
- Angular中懒加载一个模块并动态创建显示该模块下声明的组件
angular中支持可以通过路由来懒加载某些页面模块已达到减少首屏尺寸, 提高首屏加载速度的目的. 但是这种通过路由的方式有时候是无法满足需求的. 比如, 点击一个按钮后显示一行工具栏, 这个工具栏组 ...
- webpack多页面开发与懒加载hash解决方案
之前讨论了webpack的hash与chunkhash的区别以及各自的应用场景,如果是常规单页面应用的话,上篇文章提供的方案是没有问题的.但是前端项目复杂多变,应对复杂多页面项目时,我们不得不继续踩w ...
- 在webpack中使用Code Splitting--代码分割来实现vue中的懒加载
当Vue应用程序越来越大,使用Webpack的代码分割来懒加载组件,路由或者Vuex模块, 只有在需要时候才加载代码. 我们可以在Vue应用程序中在三个不同层级应用懒加载和代码分割: 组件,也称为异步 ...
- 18-Angular 自定义模块以及配置路由模块懒加载
新建项目,新建几个子模块,实现懒加载 用户.商品.文章 新建这三个模块 创建模块的时候后面加 --routing.会自动生成模块的路由文件 先删掉. 重新创建模块带routing 这样就会生成两个文件 ...
- ionic3 懒加载在微信上缓存的问题
1.懒加载是什么? 在ionic2中所有的组件.模块.服务.管道等都堆积在app.module.ts模块中,在页面初始化的时候会一次性加载所有的资源,导致资源过大,页面渲染缓慢,也导致app.modu ...
随机推荐
- Java防止SQL注入2(通过filter过滤器功能进行拦截)
首先说明一点,这个过滤器拦截其实是不靠谱的,比如说我的一篇文章是介绍sql注入的,或者评论的内容是有关sql的,那会过滤掉:且如果每个页面都经过这个过滤器,那么效率也是非常低的. 如果是要SQL注入拦 ...
- ijkplayer demo效果图
如下截图所示,用https://github.com/Bilibili/ijkplayer.git源码编译生成的ijkplayer app效果图,是这样的吗?有没有朋友指点下?
- Clang: Undefined symbols, but it is there using nm.
https://stackoverflow.com/questions/36662920/xcode-clang-link-build-dynamic-framework-or-dylib-not-e ...
- javascript 日期操作
1.获取指定年月有多少周 /** * 获得一个月的周数 * @param {} y {xxxx}4位数 * @param {} m {0-11} * @return {} */ function ge ...
- mysql中Invalid default value for 'stime'问题
在执行mysql数据库时报错 CREATE TABLE `advert_schedule_time` ( `advert_id` int(11) NOT NULL DEFAULT '0' COMMEN ...
- String StringBuffer StringBuilder
package com.test; import java.util.Date; /*** * * // 输出的结果是:// 来一个测试// 来一个测试如果只输出这句就证明了String是不可变的// ...
- 第一届山东省ACM——Balloons(java)
Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...
- [Algorithm & NLP] 文本深度表示模型——word2vec&doc2vec词向量模型
深度学习掀开了机器学习的新篇章,目前深度学习应用于图像和语音已经产生了突破性的研究进展.深度学习一直被人们推崇为一种类似于人脑结构的人工智能算法,那为什么深度学习在语义分析领域仍然没有实质性的进展呢? ...
- [cocos] ( 01 ) cocos2d-x 3.x 开发 环境配置
有几个需要注意的问题 Windows上使用时, Unable to execute dex: Multiple dex files define 在eclipse中libcoco2dx的Java Bu ...
- 【bzoj1231】[Usaco2008 Nov]mixup2 混乱的奶牛
题目描述 混乱的奶牛[Don Piele, 2007]Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S_i <= ...