好了,现进入正题,在 AngularJs 实现动态(懒)加载主要是依赖于3个主JS文件和一段依赖的脚本。

实现的过程主要是引用3个主要的JS文件

  1. <script src="angular/1.4.8/angular/angular.min.js"></script>
  2. <script src="angular/ui-router/release/angular-ui-router.min.js"></script>
  3. <script src="angular/oclazyload/src/ocLazyLoad.min.js"></script>

然后通过 APP 配置,将依赖的脚本进行注入操作

  1. var app = angular.module('pkcms', ["ui.router", "oc.lazyLoad"]);
  2. app.config(["$provide", "$compileProvider", "$controllerProvider", "$filterProvider",
  3. function ($provide, $compileProvider, $controllerProvider, $filterProvider) {
  4. app.controller = $controllerProvider.register;
  5. app.directive = $compileProvider.directive;
  6. app.filter = $filterProvider.register;
  7. app.factory = $provide.factory;
  8. app.service = $provide.service;
  9. app.constant = $provide.constant;
  10. }]);
  11. // 按模块化加载其他的脚本文件
  12. app.constant('Modules_Config', [
  13. {
  14. name: 'treeControl',
  15. serie: true,
  16. files: [
  17. "Scripts/angular-bootstrap/ui-bootstrap-tpls-0.14.3.min.js"
  18. ]
  19. }
  20. ]);
  21. app.config(["$ocLazyLoadProvider","Modules_Config",routeFn]);
  22. function routeFn($ocLazyLoadProvider,Modules_Config){
  23. $ocLazyLoadProvider.config({
  24. debug:false,
  25. events:false,
  26. modules:Modules_Config
  27. });
  28. };

以上是初始化动态加载的配置过程。

接着是建立路由

  1. "use strict"
  2. app.config(["$stateProvider","$urlRouterProvider",routeFn]);
  3. function routeFn($stateProvider,$urlRouterProvider){
  4. $urlRouterProvider.otherwise("/main");
  5. $stateProvider
  6. .state("main",{
  7. url:"/main",
  8. templateUrl:"views/main.html",
  9. controller:"mainCtrl",
  10. controllerAs:"main",
  11. resolve:{
  12. deps:["$ocLazyLoad",function($ocLazyLoad){
  13. return $ocLazyLoad.load("controllers/main.js");
  14. }]
  15. }
  16. })
  17. .state("adminUser",{
  18. url:"/adminUser",
  19. templateUrl:"views/adminUser.html",
  20. controller:"adminUserCtrl",
  21. controllerAs:"adminUser",
  22. resolve:{
  23. deps:["$ocLazyLoad",function($ocLazyLoad){
  24. return $ocLazyLoad.load("controllers/adminUser.js");
  25. }]
  26. }
  27. })
  28. };

最后是按路由配置的在对应目录下建2个HTML页面文件和2个JS文件用做测试
main.html

  1. <div>
  2. {{main.value}}
  3. </div>

adminUser.html

  1. <div>
  2. {{adminUser.value}}
  3. </div>

main.js

  1. /**
  2. * mainCtrl
  3. * Created by pkcms.cn on 2016/6/24.
  4. */
  5. (function () {
  6. "use strict"
  7. app.controller("mainCtrl", mainCtrlFn);
  8. function mainCtrlFn() {
  9. this.value = "Hello World";
  10. }
  11. }())

adminUser.js

  1. /**
  2. * adminUserCtrlFn
  3. * Created by pkcms.cn on 2016/6/24.
  4. */
  5. (function () {
  6. app.controller('adminUserCtrl',adminUserCtrlFn);
  7. function adminUserCtrlFn() {
  8. this.value = "welcome to admin user";
  9. }
  10. }());

github url :https://github.com/366065186/angularjs-oclazyload

转载请注明:PKCMS博客 » AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖

AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖的更多相关文章

  1. AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖-转

    http://blog.csdn.net/zhangh8627/article/details/51752872 AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖 标签:  ...

  2. AngularJs 动态加载模块和依赖

    最近项目比较忙额,白天要上班,晚上回来还需要做Angular知识点的ppt给同事,毕竟年底要辞职了,项目的后续开发还是需要有人接手的,所以就占用了晚上学习的时间.本来一直不打算写这些第三方插件的学习笔 ...

  3. Angular ui-roter 和AngularJS 通过 ocLazyLoad 实现动态(懒)加载模块和依赖

    什么是ui-router ui-router是AngularUI库最有用的组件之一(AngularUI库由AngularJS社区构建).它是一个第三方路由框架,允许通过状态机制组织接口,而不是简单的U ...

  4. angular5给懒加载模块添加loading

    在根组件的构造函数中直接执行: this.router.events.subscribe(event=>{ if(event instanceof RouteConfigLoadEnd) { t ...

  5. Angular中懒加载一个模块并动态创建显示该模块下声明的组件

    angular中支持可以通过路由来懒加载某些页面模块已达到减少首屏尺寸, 提高首屏加载速度的目的. 但是这种通过路由的方式有时候是无法满足需求的. 比如, 点击一个按钮后显示一行工具栏, 这个工具栏组 ...

  6. webpack多页面开发与懒加载hash解决方案

    之前讨论了webpack的hash与chunkhash的区别以及各自的应用场景,如果是常规单页面应用的话,上篇文章提供的方案是没有问题的.但是前端项目复杂多变,应对复杂多页面项目时,我们不得不继续踩w ...

  7. 在webpack中使用Code Splitting--代码分割来实现vue中的懒加载

    当Vue应用程序越来越大,使用Webpack的代码分割来懒加载组件,路由或者Vuex模块, 只有在需要时候才加载代码. 我们可以在Vue应用程序中在三个不同层级应用懒加载和代码分割: 组件,也称为异步 ...

  8. 18-Angular 自定义模块以及配置路由模块懒加载

    新建项目,新建几个子模块,实现懒加载 用户.商品.文章 新建这三个模块 创建模块的时候后面加 --routing.会自动生成模块的路由文件 先删掉. 重新创建模块带routing 这样就会生成两个文件 ...

  9. ionic3 懒加载在微信上缓存的问题

    1.懒加载是什么? 在ionic2中所有的组件.模块.服务.管道等都堆积在app.module.ts模块中,在页面初始化的时候会一次性加载所有的资源,导致资源过大,页面渲染缓慢,也导致app.modu ...

随机推荐

  1. Sensitive directory/file Integrity Monitoring and Checking

    catalogue . OSSEC . HashSentry: Host-Based IDS in Python . Afick . 检测流程 1. OSSEC OSSEC is an Open So ...

  2. POJ 1797 Heavy Transportation(最大生成树/最短路变形)

    传送门 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 31882   Accept ...

  3. sql语句,order by

    ORDER BY子句必须出现在SELECT中的最后一个子句. 在排序的列中NULL值被认为是最大的. 在SQL语句中给表达式定义别名是一个好习惯. 多列排序时不管升序还是降序,每个列需要单独设置

  4. 练习用基础SQL语句

    http://www.cnblogs.com/zxlovenet/p/3728842.html 本文语句大部分SQL语句来自<数据库系统概论>(第四版)王珊&萨师煊 ,是我们上课用 ...

  5. selenium 打开浏览器

    import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebE ...

  6. 换行的css属性

    //正常换行  word-break:keep-all;word-wrap:normal; //下面这行是自动换行  word-break:break-all;word-wrap:break-word ...

  7. ThinkPHP Where 条件中使用表达式

    本文转自:这里 Where 条件表达式格式为: $map['字段名'] = array('表达式', '操作条件'); 其中 $map 是一个普通的数组变量,可以根据自己需求而命名.上述格式中的表达式 ...

  8. System.Web.HttpRequestValidationException: A potentially dangerous Request.F

    ASP.NET .0验证请求 System.Web.HttpRequestValidationException: A potentially dangerous Request.F System.W ...

  9. V4L2框架分析学习一

    转载于http://www.techbulo.com/1193.html 1.概述 Video4Linux2是Linux内核中关于视频设备的内核驱动框架,为上层的访问底层的视频设备提供了统一的接口.凡 ...

  10. 转载:Centos7 从零编译配置Memcached

    序言 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度. Memca ...