zoukankan      html  css  js  c++  java
  • angularjs 1.x lazyloading

    https://oclazyload.readme.io/docs

    1. 安装好后直接使用
    var myApp = angular.module("MyApp", ["oc.lazyLoad"]);
    
    1. 用来加载模块
    myApp.controller("MyCtrl", function($ocLazyLoad) {
      $ocLazyLoad.load('testModule.js');
    });
    
    1. 加载组件
      如果组件在独立的模块中就和模块差不多, 否则将要加载的组件应该是属于已定义好的模块

    2. live examples
      查看examples来了解更多的用法

    在路由中的应用

    $ocLazyLoad works well with routers and especially ui-router. Since it returns a promise, use the resolve property to make sure that your components are loaded before the view is resolved:

    $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('js/AppCtrl.js');
        }]
      }
    });
    

    If you have nested views, make sure to include the resolve from the parent to load your components in the right order:

    $stateProvider.state('parent', {
      url: "/",
      resolve: {
        loadMyService: ['$ocLazyLoad', function($ocLazyLoad) {
                 return $ocLazyLoad.load('js/ServiceTest.js');
        }]
      }
    })
    .state('parent.child', {
        resolve: {
            test: ['loadMyService', '$ServiceTest', function(loadMyService, $ServiceTest) {
                // you can use your service
                $ServiceTest.doSomething();
            }]
        }
    });
    

    It also works for sibling resolves:

    $stateProvider.state('index', {
      url: "/",
      resolve: {
        loadMyService: ['$ocLazyLoad', function($ocLazyLoad) {
                 return $ocLazyLoad.load('js/ServiceTest.js');
        }],
            test: ['loadMyService', '$serviceTest', function(loadMyService, $serviceTest) {
                // you can use your service
                $serviceTest.doSomething();
            }]
        }
    });
    

    Of course, if you want to use the loaded files immediately, you don't need to define two resolves, you can also use the injector (it works anywhere, not just in the router):

    $stateProvider.state('index', {
      url: "/",
      resolve: {
        loadMyService: ['$ocLazyLoad', '$injector', function($ocLazyLoad, $injector) {
          return $ocLazyLoad.load('js/ServiceTest.js').then(function() {
            var $serviceTest = $injector.get("$serviceTest");
            $serviceTest.doSomething();
          });
        }]
      }
    });
    
  • 相关阅读:
    mysql sum 重复计算_mysql join sum时数据重复问题及解决方案
    mysql数据库中,查询一个表的下一条数据减上一条数据的值的写法
    FROM_UNIXTIME 格式化MYSQL时间戳函数
    Mysql 中日期类型bigint和datetime互转
    mysql 按照指定字段的指定数据进行排序 filed函数
    MYSQL使用group by,如何查询出总记录数
    iptables添加开放端口
    MySQL之You can't specify target table for update in FROM clause解决办法
    epoll 使用 LT + 非阻塞 IO 和 ET + 非阻塞 IO 比较
    sizeof和strlen的比较
  • 原文地址:https://www.cnblogs.com/wancy86/p/7581144.html
Copyright © 2011-2022 走看看