AngularJs Dynamic loading modules and Dependency injection details

  • 2020-12-05 17:05:03
  • OfStack

Without further ado, let's get down to business...

First let's look at the file structure:


Angular-ocLazyLoad           --- demo folder 
  Scripts               ---  Framework and plug-in folders 
    angular-1.4.7          --- angular  Don't explain 
    angular-ui-router        --- uirouter  Don't explain 
    oclazyload           --- ocLazyload  Don't explain 
    bootstrap            --- bootstrap  Don't explain 
    angular-tree-control-master   --- angular-tree-control-master  Don't explain 
    ng-table            --- ng-table  Don't explain 
    angular-bootstrap        --- angular-bootstrap  Don't explain 
  js                 --- js folder   for demo Write the js file 
    controllers           ---  Page controller folder 
      angular-tree-control.js   --- angular-tree-control Controller code 
      default.js         --- default Controller code 
      ng-table.js         --- ng-table Controller code 
    app.config.js          ---  Module registration and configuration code 
    oclazyload.config.js      ---  Load the module configuration code 
    route.config.js         ---  Route configuration and load code 
  views                --- html Page folder 
    angular-tree-control.html    --- angular-tree-control The plugin's effect page 
    default.html          --- default page 
    ng-table.html          --- ng-table Plug-in effect page 
    ui-bootstrap.html        --- uibootstrap Plug-in effect page 
  index.html             ---  The main page 

Note: This demo does not do nested routes, but simply implements single-view routing to show the effect.

Let's look at the code on the main page:


<html>
<head>
  <meta charset="utf-8" />
  <title></title>
  <link rel="stylesheet" href="Scripts/bootstrap/dist/css/bootstrap.min.css" />
  <script src="Scripts/angular-1.4.7/angular.js"></script>
  <script src="Scripts/angular-ui-router/release/angular-ui-router.min.js"></script>
  <script src="Scripts/oclazyload/dist/ocLazyLoad.min.js"></script>
  <script src="js/app.config.js"></script>
  <script src="js/oclazyload.config.js"></script>
  <script src="js/route.config.js"></script>
</head>
<body>
<div ng-app="templateApp">
  <div>
    <a href="#/default"> The home page </a>
    <a href="#/uibootstrap" >ui-bootstrap</a>
    <a href="#/ngtable">ng-table</a>
    <a href="#/ngtree">angular-tree-control</a>
  </div>
  <div ui-view></div>
</div>
</body>
</html>

On this page, we only loaded css for bootstrap, js for angular, js for ES18en-ES19en, js for ocLazyLoad, and three configured js files.
Look at the html code for the four pages:

angular-tree-control effects page


<treecontrol tree-model="ngtree.treeData" class="tree-classic ng-cloak" options="ngtree.treeOptions">
  {{node.title}}
</treecontrol>

There is an instruction on the page to use the plug-in.

default page


<div class="ng-cloak">
  {{default.value}}
</div>

Here we are simply proving that default.js is loaded and executed correctly.

ng-table effects page


<div class="ng-cloak">
  <div class="p-h-md p-v bg-white box-shadow pos-rlt">
    <h3 class="no-margin">ng-table</h3>
  </div>
  <button ng-click="ngtable.tableParams.sorting({})" class="btn btn-default pull-right">Clear sorting</button>
  <p>
    <strong>Sorting:</strong> {{ngtable.tableParams.sorting()|json}}
  </p>
  <table ng-table="ngtable.tableParams" class="table table-bordered table-striped">
    <tr ng-repeat="user in $data">
      <td data-title="'Name'" sortable="'name'">
        {{user.name}}
      </td>
      <td data-title="'Age'" sortable="'age'">
        {{user.age}}
      </td>
    </tr>
  </table>
</div>

Here are some simple ng-ES53en effects.

ui-bootstrap effects page


<span uib-dropdown class="ng-cloak">
  <a href id="simple-dropdown" uib-dropdown-toggle>
     The drop-down box fires 
  </a>
  <ul class="uib-dropdown-menu dropdown-menu" aria-labelledby="simple-dropdown">
     Drop-down box content . So I'm just going to write an effect here to show you how to do dynamic loading 
  </ul>
</span>

Just a drop-down box effect to prove that the plug-in is loaded and used correctly.
Ok, having looked at html, let's look at the load configuration and route configuration:


"use strict"
var tempApp = angular.module("templateApp",["ui.router","oc.lazyLoad"])
.config(["$provide","$compileProvider","$controllerProvider","$filterProvider",
        function($provide,$compileProvider,$controllerProvider,$filterProvider){
          tempApp.controller = $controllerProvider.register;
          tempApp.directive = $compileProvider.register;
          tempApp.filter = $filterProvider.register;
          tempApp.factory = $provide.factory;
          tempApp.service =$provide.service;
          tempApp.constant = $provide.constant;
        }]);

The above code only relies on ui. router and ES70en. LazyLoad for module registration. The configuration also simply configured the module so that later js would recognize the method on tempApp.
Then let's look at the configuration of the ocLazyLoad load module:


"use strict"
tempApp
.constant("Modules_Config",[
  {
    name:"ngTable",
    module:true,
    files:[
      "Scripts/ng-table/dist/ng-table.min.css",
      "Scripts/ng-table/dist/ng-table.min.js"
    ]
  },
  {
    name:"ui.bootstrap",
    module:true,
    files:[
      "Scripts/angular-bootstrap/ui-bootstrap-tpls-0.14.3.min.js"
    ]
  },
  {
    name:"treeControl",
    module:true,
    files:[
      "Scripts/angular-tree-control-master/css/tree-control.css",
      "Scripts/angular-tree-control-master/css/tree-control-attribute.css",
      "Scripts/angular-tree-control-master/angular-tree-control.js"
    ]
  }
])
.config(["$ocLazyLoadProvider","Modules_Config",routeFn]);
function routeFn($ocLazyLoadProvider,Modules_Config){
  $ocLazyLoadProvider.config({
    debug:false,
    events:false,
    modules:Modules_Config
  });
};

Routing configuration:


"use strict"
tempApp.config(["$stateProvider","$urlRouterProvider","$locationProvider",routeFn]);
function routeFn($stateProvider,$urlRouterProvider,$locationProvider){
  $urlRouterProvider.otherwise("/default");
  $stateProvider
  .state("default",{
    url:"/default",
    views:{
      "":{
        templateUrl:"views/default.html",
        controller:"defaultCtrl",
        controllerAs:"default"
      }
    },
    resolve:{
      deps:["$ocLazyLoad",function($ocLazyLoad){
        return $ocLazyLoad.load("js/controllers/default.js");
      }]
    }
  })
  .state("uibootstrap",{
    url:"/uibootstrap",
    views:{
      "":{
        templateUrl:"views/ui-bootstrap.html"
      }
    },
    resolve:{
      deps:["$ocLazyLoad",function($ocLazyLoad){
        return $ocLazyLoad.load("ui.bootstrap");
      }]
    }
  })
  .state("ngtable",{
    url:"/ngtable",
    views:{
      "":{
        templateUrl:"views/ng-table.html",
        controller:"ngTableCtrl",
        controllerAs:"ngtable"
      }
    },
    resolve:{
      deps:["$ocLazyLoad",function($ocLazyLoad){
        return $ocLazyLoad.load("ngTable").then(
          function(){
            return $ocLazyLoad.load("js/controllers/ng-table.js");
          }
        );
      }]
    }
  })
  .state("ngtree",{
    url:"/ngtree",
    views:{
      "":{
        templateUrl:"views/angular-tree-control.html",
        controller:"ngTreeCtrl",
        controllerAs:"ngtree"
      }
    },
    resolve:{
      deps:["$ocLazyLoad",function($ocLazyLoad){
        return $ocLazyLoad.load("treeControl").then(
          function(){
            return $ocLazyLoad.load("js/controllers/angular-tree-control.js");
          }
        );
      }]
    }
  })
};

The simple implementation of the drop-down box of ES84en-ES85en does not require a controller, so let's just look at the controller js of ES86en-ES87en and ES88en-ES89en-ES90en:

ng-table.js


(function(){
"use strict"
tempApp
.controller("ngTableCtrl",["$location","NgTableParams","$filter",ngTableCtrlFn]);
function ngTableCtrlFn($location,NgTableParams,$filter){
  var vm = this;
  // data 
  var data = [{ name: "Moroni", age: 50 },
         { name: "Tiancum ", age: 43 },
         { name: "Jacob", age: 27 },
         { name: "Nephi", age: 29 },
         { name: "Enos", age: 34 },
         { name: "Tiancum", age: 43 },
         { name: "Jacob", age: 27 },
         { name: "Nephi", age: 29 },
         { name: "Enos", age: 34 },
         { name: "Tiancum", age: 43 },
         { name: "Jacob", age: 27 },
         { name: "Nephi", age: 29 },
         { name: "Enos", age: 34 },
         { name: "Tiancum", age: 43 },
         { name: "Jacob", age: 27 },
         { name: "Nephi", age: 29 },
         { name: "Enos", age: 34 }];
  vm.tableParams = new NgTableParams(  //  Merge the default configuration and url parameter 
    angular.extend({
      page: 1,      //  The first 1 page 
      count: 10,     //  The amount of data per page 
      sorting: {
        name: 'asc'   //  The default sort 
      }
    },
    $location.search())
    ,{
      total: data.length, //  The total number of data 
      getData: function ($defer, params) {
        $location.search(params.url()); //  Put the parameter to url , the implementation of refreshing the page will not jump back to the first 1 Page and default configuration 
        var orderedData = params.sorting ?
            $filter('orderBy')(data, params.orderBy()) :data;
        $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
      }
    }
  );
};
})();

angular-tree-control.js


<html>
<head>
  <meta charset="utf-8" />
  <title></title>
  <link rel="stylesheet" href="Scripts/bootstrap/dist/css/bootstrap.min.css" />
  <script src="Scripts/angular-1.4.7/angular.js"></script>
  <script src="Scripts/angular-ui-router/release/angular-ui-router.min.js"></script>
  <script src="Scripts/oclazyload/dist/ocLazyLoad.min.js"></script>
  <script src="js/app.config.js"></script>
  <script src="js/oclazyload.config.js"></script>
  <script src="js/route.config.js"></script>
</head>
<body>
<div ng-app="templateApp">
  <div>
    <a href="#/default"> The home page </a>
    <a href="#/uibootstrap" >ui-bootstrap</a>
    <a href="#/ngtable">ng-table</a>
    <a href="#/ngtree">angular-tree-control</a>
  </div>
  <div ui-view></div>
</div>
</body>
</html>
0

Let's ignore default.js. After all, there is only "Hello Wrold".

Above is the entire content of this article, I hope to help you with your study.


Related articles: