博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angularjs 1.x lazyloading
阅读量:6442 次
发布时间:2019-06-23

本文共 2337 字,大约阅读时间需要 7 分钟。

  1. 安装好后直接使用
var myApp = angular.module("MyApp", ["oc.lazyLoad"]);
  1. 用来加载模块
myApp.controller("MyCtrl", function($ocLazyLoad) {  $ocLazyLoad.load('testModule.js');});
  1. 加载组件

    如果组件在独立的模块中就和模块差不多, 否则将要加载的组件应该是属于已定义好的模块

  2. 查看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();      });    }]  }});

转载地址:http://bhdwo.baihongyu.com/

你可能感兴趣的文章
Swoole WebSocket 的应用
查看>>
219. 单页应用 会话管理(session、cookie、jwt)
查看>>
【比赛】百度之星2017 初赛Round B
查看>>
AFNetworking之AFSecurityPolicy深入学习
查看>>
JavaScript中的“this”
查看>>
Java中abstract class和interface的区别
查看>>
(OkHttp3+Gson)用MVP模式实现天气预报小demo
查看>>
5G时代下,优质内容依然短视频源码的核心竞争力
查看>>
别再写getter,setter方法了,用Lombok来简化你的代码吧
查看>>
依赖注入
查看>>
Anconda 3.7安装以及使用详细教程
查看>>
scala 学习笔记二 方法与函数
查看>>
微软职位内部推荐-SOFTWARE ENGINEER II
查看>>
如何用公式编辑器编辑直角三角形符号
查看>>
每日一个Linux命令 地址
查看>>
UI---设置Activity背景为透明
查看>>
晒晒名企大公司的工资收入
查看>>
【DOM编程艺术】显示"文献来源链接表"
查看>>
关于css
查看>>
HTML5 web workers
查看>>