In Focus

AngularJS Reloaded - Lazy Loading Files

In this article, I will show you how to load your scripts and styles, using AngularJS and UI Router Resolve when it’s required.

In this article, I will show you how to load your scripts and styles, using AngularJS and UI Router Resolve, when it's required.
 
Let's face the problem
 
You have a page "A" that uses jqGrid plugin and two other pages, "B" and "C," that do not use the plugin. You normally reference jqGrid plugin in your master page, so the pages "A", "B" and "C" will load the script plugin but only page "A" will need it. This is a network performance issue and it becomes complex with many scripts.
 
The Solution
 
Use a combination of lazy loading AngularJS plugin and AngularJS UI Router Resolve to do all the necessary steps before the page loads.
 
Let's see the code given below.
  1. var definitions = function () {  
  2.     var baseFolder = '';  
  3.     return {  
  4.         grid: {  
  5.             devDependencies: [  
  6.                 baseFolder + 'Css/plugins/jQueryUI/jquery-ui-1.10.4.custom.min.css',  
  7.                 baseFolder + 'Css/plugins/jqGrid/ui.jqgrid.css',  
  8.                 baseFolder + 'Css/style.jqGrid.css',  
  9.                 baseFolder + 'Scripts/plugins/jqGrid/i18n/grid.locale-pt.js',  
  10.                 baseFolder + 'Scripts/plugins/jqGrid/jquery.jqGrid.min.js',  
  11.                 baseFolder + 'Scripts/plugins/jquery-ui/jquery-ui.min.js'  
  12.             ]  
  13.         }  
  14.     };  
  15. } ();   
The "definitions" hold a configuration for any plugin and scripts/css dependencies.
  1. (function () {  
  2.   
  3.     class AppRoutes {  
  4.   
  5.         constructor(  
  6.             private utilProvider: App.Util,  
  7.             private $stateProvider: ng.ui.IStateProvider,  
  8.             private $urlRouterProvider: ng.ui.IUrlRouterProvider  
  9.         ) {  
  10.             let genericRoute = new App.Core.State.GenericState(this.utilProvider);  
  11.   
  12.             this.$urlRouterProvider.otherwise("/login");  
  13.             this.$stateProvider.state('homemenu', genericRoute.getState({  
  14.                 url: '/',  
  15.                 views: {  
  16.                     root: {  
  17.                         template: 'The page does not use GRID scripts. Go to <a ui-sref="homeperson">Person Page</a>'  
  18.                     }  
  19.                 }  
  20.             }));  
  21.             this.$stateProvider.state('homeperson', genericRoute.getState({  
  22.                 url: '/person',  
  23.                 dependencies: ['grid'],  
  24.                 views: {  
  25.                     root: {  
  26.                         template: 'That page uses GRID scripts. Back to <a ui-sref="homemenu">Home Page</a>'  
  27.                     }  
  28.                 }  
  29.             }));  
  30.         }  
  31.     }  
  32.   
  33.     AppRoutes.$inject = [  
  34.         'utilProvider',  
  35.         '$stateProvider',  
  36.         '$urlRouterProvider'  
  37.     ];  
  38.   
  39.     angular  
  40.         .module('app')  
  41.         .config(AppRoutes);  
  42.   
  43. })();   
The code given above helps to configure the routes for this article. Just the "/person" route uses "grid" plugin as a dependency. The "getState" method helps you to configure the lazy loading for the dependency and verify in the "definition" object to know which scripts and styles will load.
 
Try it yourself

Access the home page.
 

Check in Fiddler, no scripts and styles for GRID have been loaded.
 
 
Click Person Page link and it will navigate to person page.
 
 
Check in Fiddler. For this route, our GRID scripts styles have been loaded successfully.
 
 

Dowload full source code.