zoukankan      html  css  js  c++  java
  • AngularJs 通过 ocLazyLoad 实现动态(懒)加载模块和依赖-转

    http://blog.csdn.net/zhangh8627/article/details/51752872

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

    标签: AngularJSocLazyLoad动态加载懒加载

    最近一直在学习 AngularJS 的实践过程中,之前虽然也在学,但总因为工作方面的各种原因一直是断断续续的,虽然之前也一直看但总找不到入手的感觉,可能是自己的心太高想着一下子就能掌握,还有一个原因是之前用习惯了 jQuery 的模式,在 AngularJs 下不太适应所以在学习过程中就遇到许多问题,就像今天解决的动态加载问题,在 AngularJs 常规的教程书籍中很少有提到,有的甚至都没有,都是用常规的一次性的加载方式,这种方式只适合用在教学和小型项目中,在中大型项目中不推荐使用,最好是使用动态(懒)加载的方式。这种方式的好处就不用说有经验的程序员都知道。

    今天是下了决心把这个问题给解决了,这个解决的过程还得感谢我之前的一位做前端的同事,他比我早学习 AngularJs。虽然他在自己的博客上也记录,但他上面的记录含有一些 C#语言的元素在里面会有看不明白的情况,我这里直接用纯前端语言做下整理。

    好了,现进入正题,在 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

  • 相关阅读:
    MySQL大数据分页调优实践
    CentOS 搭建L2TP
    CentOS 搭建SS5
    CentOS 搭建PPTP
    CentOS 防火墙 firewalld
    CentOS 初始化账号
    nginx升级与回退
    Python
    python
    linux
  • 原文地址:https://www.cnblogs.com/sxz2008/p/6287958.html
Copyright © 2011-2022 走看看