Home > Web Front-end > JS Tutorial > body text

AngularJS dynamic loading module and dependency method analysis

高洛峰
Release: 2016-12-07 14:01:50
Original
1580 people have browsed it

The example in this article describes the method of dynamically loading modules and dependencies in AngularJS. Share it with everyone for your reference, the details are as follows:

Foreword

Since AngularJS is a single-page application framework, under normal circumstances, all CSS and JavaScript files will be loaded when accessing the page. When there are not many files, the page startup speed will not have much impact. But once there are too many files or the loaded third-party library is relatively large, it will affect the page startup speed. Therefore, when the application scale is large, the number of files is relatively large, or the third-party libraries loaded are relatively large, dynamically loading JS or dynamically loading modules will greatly improve the startup speed of the page. This article will introduce how to use ocLazyLoad to implement dynamic loading.

Prepare

AngularJS dynamic loading relies on a third-party library: ocLazyLoad. ocLazyLoad is a third-party library that supports AngularJS to dynamically load modules, services, directives, and static files.

Install ocLazyLoad

Can be installed through npm or bower

npm install oclazyload
bower install oclazyload
Copy after login

Add ocLazyLoad module to your application

angular.module('myApp',['oc.lazyLoad']);
Copy after login

Configure ocLazyLoad

Configure $ocLazyLoadProvider in the config function, The configuration file is as follows

.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider){
  $ocLazyLoadProvider.config({
    debug: true,
    events: true,
    modules: [
      {
        name: 'TestModule',
        files: ['test.js']
      }
    ]
  })
}])
Copy after login

debug: used to enable debug mode. Boolean value, default is false. When debug mode is enabled, $ocLazyLoad will print out all errors to the console.
events: When you dynamically load a module, $ocLazyLoad will broadcast the corresponding event. Boolean value, defaults to false.
modules: used to define the modules you need to load dynamically. Each module must have a unique name.
Modules must be in the form of an array, and files must also exist in the form of an array, even if only one file needs to be loaded

Load the module in the route

.config(['$routeProvider', function($routeProvider) {
    $routeProvider.otherwise('/index');
    $routeProvider.when('/index', {
      templateUrl: 'index.html',
      controller: 'IndexController',
      resolve: { // resolve 里的属性如果返回的是 promise对象,那么resolve将会在view加载之前执行
        loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
          // 在这里可以延迟加载任何文件或者刚才预定义的modules
          return $ocLazyLoad.load('TestModule'); //加载刚才定义的TestModule
          /*return $ocLazyLoad.load([  // 如果要加载多个module,需要写成数组的形式
            'TestModule',
            'MainModule'
            ]);*/
        }]
      }
    })
}])
Copy after login

The properties set by resolve can be injected into the Controller. If resolve returns promise objects, they will be executed before the controller is loaded and $routeChangeSuccess is triggered.

$ocLazyLoad uses this principle hack to perform dynamic loading. The value of

resolve can be:

* key, the value of key is the name of the dependency that will be injected into the Controller;
* factory, which can be the name of a service or a return value, which will A function that is injected into the controller or a promise object that can be resolved.

Through this configuration, AngularJS can dynamically load modules and dependencies. However, ocLazyLoad provides richer functions, not only dynamic loading of modules and dependencies, but also dynamic loading of services, direct, etc.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!