ocLazyLoad [](https://travis-ci.org/ocombe/ocLazyLoad) ========== Load modules on demand (lazy load) in AngularJS ## Table of contents 1. [Key features](#key-features) 2. [Usage](#usage) 3. [Service](#service) 4. [Directive](#directive) 5. [Dependency injection](#bonus-use-dependency-injection) 6. [Configuration](#configuration) 7. [Router usage](#works-well-with-your-router) 8. [Other function](#other-functions) 9. [Contribute](#contribute) 10. [Karma & unit tests](#karma--unit-tests) ## Key features - Dependencies are automatically loaded - Debugger friendly (no eval code) - The ability to mix normal boot and load on demand - Load via the service or the directive - Use the embedded async loader or use your own (requireJS, ...) - Load js (angular or not) / css / templates files - Compatible with AngularJS 1.2.x and 1.3.x ## Usage - Put ocLazyLoad.js into you project - Add the module ```oc.lazyLoad``` to your application (you can install it with `bower install oclazyload` or `npm install oclazyload`) - Load on demand: With $ocLazyLoad you can load angular modules, but if you want to load any component (controllers / services / filters / ...) without defining a new module it's entirely possible (just make sure that you define this component within an existing module). There are multiple ways to use `$ocLazyLoad` to load your files, just choose the one that fits you the best. ###### More examples / tutorials / articles You can find three basic examples in the example folder. If you need more, check out these links: - Lazy loading with requirejs, ocLazyLoad and ui-router: [using the templateProvider](http://plnkr.co/edit/OGvi01?p=preview) / [using the uiRouterDecorator](http://plnkr.co/edit/6CLDsz?p=preview) - by @gilbox - Lazy Loading ui-router states with requirejs, ocLazyLoad and ui-router-extras futureStates, [part 1](http://bardo.io/posts/oclazyload-future-states/) / [part 2](http://bardo.io/posts/ng-deferred-bootstrap-like-with-oclazyload/) - by @kbdaitch - Lazy loading Angular modules with ocLazyLoad, [part 1](https://egghead.io/lessons/angularjs-lazy-loading-angular-modules-with-oclazyload) / [part 2](https://egghead.io/lessons/angularjs-lazy-loading-modules-with-ui-router-and-oclazyload) / [part 3](https://egghead.io/lessons/angularjs-simple-lazy-loaded-angular-module-syntax-with-oclazyload) / [part 4](https://egghead.io/lessons/angularjs-lazy-loading-non-angular-libraries-with-oclazyload) - An AngularJS lesson by [@johnlindquist](https://twitter.com/johnlindquist) on [egghead.io](https://egghead.io/) ### Service You can include `$ocLazyLoad` and use the function `load` which returns a promise. It supports a single dependency (object) or multiple dependencies (array of objects). Load one or more modules & components with one file: ```js $ocLazyLoad.load('testModule.js'); ``` Load one or more modules & components with multiple files: ```js $ocLazyLoad.load(['testModule.js', 'testModuleCtrl.js', 'testModuleService.js']); ``` Load one or more modules with multiple files and specify a type where necessary: Note: When using the requireJS style formatting (with `js!` at the beginning for example), do not specify a file extension. Use one or the other. ```js $ocLazyLoad.load([ 'testModule.js', {type: 'css', path: 'testModuleCtrl'}, {type: 'html', path: 'testModuleCtrl.html'}, {type: 'js', path: 'testModuleCtrl'}, 'js!testModuleService', 'less!testModuleLessFile' ]); ``` You can also load external libs (not angular): ```js $ocLazyLoad.load(['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js', 'anotherModule.js']); ``` You can also load css and template files: ```js $ocLazyLoad.load([ 'bower_components/bootstrap/dist/js/bootstrap.js', 'bower_components/bootstrap/dist/css/bootstrap.css', 'partials/template1.html' ]); ``` If you want to load templates, the template file should be an html (or htm) file with regular [script templates](https://docs.angularjs.org/api/ng/directive/script). It looks like this: ```html ``` You can put more than one template script in your template file, just make sure to use different ids: ```html ``` There are two ways to define config options for the load function. You can use a second optional parameter that will define configs for all the modules that you will load, or you can define optional parameters to each module. For example, these are equivalent: ```js $ocLazyLoad.load([{ files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js'], cache: false },{ files: ['anotherModule.js'], cache: true }]); ``` And ```js $ocLazyLoad.load( ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js', 'anotherModule.js'], {cache: false} ); ``` If you load a template with the native template loader, you can use any parameter from the $http service (check: https://docs.angularjs.org/api/ng/service/$http#usage). ```js $ocLazyLoad.load( ['partials/template1.html', 'partials/template2.html'], {cache: false, timeout: 5000} ); ``` The existing parameters that you can use are `cache`, `reconfig`, `rerun`, `serie` and `insertBefore`. The parameter `cache: false` works for all native loaders (**all requests are cached by default**): ```js $ocLazyLoad.load({ cache: false, files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js'] }); ``` By default, if you reload a module, the config block won't be invoked again (because often it will lead to unexpected results). But if you really need to execute the config function again, use the parameter `reconfig: true`: ```js $ocLazyLoad.load({ reconfig: true, files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js'] }); ``` The same problem might happen with run blocks, use `rerun: true` to rerun the run blocks: ```js $ocLazyLoad.load({ rerun: true, files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js'] }); ``` By default the native loaders will load your files in parallel. If you need to load your files in serie, you can use `serie: true`: ```js $ocLazyLoad.load({ serie: true, files: [ 'bower_components/angular-strap/dist/angular-strap.js', 'bower_components/angular-strap/dist/angular-strap.tpl.js' ] }); ``` The files, by default, will be inserted before the last child of the `head` element. You can override this by using `insertBefore: 'cssSelector'`: ```js $ocLazyLoad.load({ insertBefore: '#load_css_before', files: ['bower_components/bootstrap/dist/css/bootstrap.css'] }); ``` ### Directive The directive usage is very similar to the service. The main interest here is that the content will be included and compiled once your modules have been loaded. This means that you can use directives that will be lazy loaded inside the oc-lazy-load directive. Use the same parameters as a service: ```html