


AngularJs dynamically loaded modules and dependency injection detailed explanation_AngularJS
Without further ado, let’s get to the point...
First let’s take a look at the file structure:
Angular-ocLazyLoad --- demo文件夹 Scripts --- 框架及插件文件夹 angular-1.4.7 --- angular 不解释 angular-ui-router --- uirouter 不解释 oclazyload --- ocLazyload 不解释 bootstrap --- bootstrap 不解释 angular-tree-control-master --- angular-tree-control-master 不解释 ng-table --- ng-table 不解释 angular-bootstrap --- angular-bootstrap 不解释 js --- js文件夹 针对demo写的js文件 controllers --- 页面控制器文件夹 angular-tree-control.js --- angular-tree-control控制器代码 default.js --- default控制器代码 ng-table.js --- ng-table控制器代码 app.config.js --- 模块注册及配置代码 oclazyload.config.js --- 加载模块配置代码 route.config.js --- 路由配置及加载代码 views --- html页面文件夹 angular-tree-control.html --- angular-tree-control插件的效果页面 default.html --- default页面 ng-table.html --- ng-table插件效果页面 ui-bootstrap.html --- uibootstrap插件效果页面 index.html --- 主页面
Note: This demo does not implement nested routing, but simply implements single-view routing to show the effect.
Let’s look at the code of 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">主页</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 bootstrap’s css, angular’s js, ui-router’s js, ocLazyLoad’s js, and 3 configured js files.
Take another look at the html code of the four pages:
angular-tree-control effect page
<treecontrol tree-model="ngtree.treeData" class="tree-classic ng-cloak" options="ngtree.treeOptions"> {{node.title}} </treecontrol>
There are instructions for using the plug-in on the page.
default page
<div class="ng-cloak"> {{default.value}} </div>
Here we just use it to prove that default.js is loaded and executed correctly.
ng-table effect 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-table effects.
ui-bootstrap effect page
<span uib-dropdown class="ng-cloak"> <a href id="simple-dropdown" uib-dropdown-toggle> 下拉框触发 </a> <ul class="uib-dropdown-menu dropdown-menu" aria-labelledby="simple-dropdown"> 下拉框内容.这里写个效果证明实现动态加载即可 </ul> </span>
Only a drop-down box effect is written here to prove that the plug-in is loaded and used correctly.
Okay, after reading the html, let’s take a look at the loading configuration and routing 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 oc.LazyLoad to register the module. The configuration is just a simple configuration of the module so that the subsequent js can recognize the methods on tempApp.
Then let’s take a look at the configuration of the ocLazyLoad loading 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 ui-bootstrap's drop-down box does not require a controller, so let's just look at the controller js of ng-table and angular-tree-control:
ng-table.js
(function(){ "use strict" tempApp .controller("ngTableCtrl",["$location","NgTableParams","$filter",ngTableCtrlFn]); function ngTableCtrlFn($location,NgTableParams,$filter){ var vm = this; //数据 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( // 合并默认的配置和url参数 angular.extend({ page: 1, // 第一页 count: 10, // 每页的数据量 sorting: { name: 'asc' // 默认排序 } }, $location.search()) ,{ total: data.length, // 数据总数 getData: function ($defer, params) { $location.search(params.url()); // 将参数放到url上,实现刷新页面不会跳回第一页和默认配置 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
(function(){ "use strict" tempApp .controller("ngTreeCtrl",ngTreeCtrlFn); function ngTreeCtrlFn(){ var vm = this; //树数据 vm.treeData = [ { id:"1", title:"标签1", childList:[ { id:"1-1", title:"子级1", childList:[ { id:"1-1-1", title:"再子级1", childList:[] } ] }, { id:"1-2", title:"子级2", childList:[ { id:"1-2-1", title:"再子级2", childList:[ { id:"1-2-1-1", title:"再再子级1", childList:[] } ] } ] }, { id:"1-3", title:"子级3", childList:[] } ] }, { id:"2", title:"标签2", childList:[ { id:"2-1", title:"子级1", childList:[] }, { id:"2-2", title:"子级2", childList:[] }, { id:"2-3", title:"子级3", childList:[] } ]} , { id:"3", title:"标签3", childList:[ { id:"3-1", title:"子级1", childList:[] }, { id:"3-2", title:"子级2", childList:[] }, { id:"3-3", title:"子级3", childList:[] } ] } ]; //树配置 vm.treeOptions = { nodeChildren:"childList", dirSelectable:false }; }; })();
Let us ignore default.js, after all there is only "Hello Wrold" in it.
The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



This article will take you through dependency injection, introduce the problems that dependency injection solves and its native writing method, and talk about Angular's dependency injection framework. I hope it will be helpful to you!

Introduction to the method of using dependency injection (DependencyInjection) in the Phalcon framework: In modern software development, dependency injection (DependencyInjection) is a common design pattern aimed at improving the maintainability and testability of the code. As a fast and low-cost PHP framework, the Phalcon framework also supports the use of dependency injection to manage and organize application dependencies. This article will introduce you how to use the Phalcon framework

Javascript is a very unique language. It is unique in terms of the organization of the code, the programming paradigm of the code, and the object-oriented theory. The issue of whether Javascript is an object-oriented language that has been debated for a long time has obviously been There is an answer. However, even though Javascript has been dominant for twenty years, if you want to understand popular frameworks such as jQuery, Angularjs, and even React, just watch the "Black Horse Cloud Classroom JavaScript Advanced Framework Design Video Tutorial".

In Go, the dependency injection (DI) mode is implemented through function parameter passing, including value passing and pointer passing. In the DI pattern, dependencies are usually passed as pointers to improve decoupling, reduce lock contention, and support testability. By using pointers, the function is decoupled from the concrete implementation because it only depends on the interface type. Pointer passing also reduces the overhead of passing large objects, thereby reducing lock contention. Additionally, DI pattern makes it easy to write unit tests for functions using DI pattern since dependencies can be easily mocked.

For testing dependency injection using JUnit, the summary is as follows: Use mock objects to create dependencies: @Mock annotation can create mock objects of dependencies. Set test data: The @Before method runs before each test method and is used to set test data. Configure mock behavior: The Mockito.when() method configures the expected behavior of the mock object. Verify results: assertEquals() asserts to check whether the actual results match the expected values. Practical application: You can use a dependency injection framework (such as Spring Framework) to inject dependencies, and verify the correctness of the injection and the normal operation of the code through JUnit unit testing.

In today's information age, websites have become an important tool for people to obtain information and communicate. A responsive website can adapt to various devices and provide users with a high-quality experience, which has become a hot spot in modern website development. This article will introduce how to use PHP and AngularJS to build a responsive website to provide a high-quality user experience. Introduction to PHP PHP is an open source server-side programming language ideal for web development. PHP has many advantages, such as easy to learn, cross-platform, rich tool library, development efficiency

Answer: In Go language, dependency injection can be implemented through interfaces and structures. Define an interface that describes the behavior of dependencies. Create a structure that implements this interface. Inject dependencies through interfaces as parameters in functions. Allows easy replacement of dependencies in testing or different scenarios.

Answer: Dependency injection and service containers in PHP help to flexibly manage dependencies and improve code testability. Dependency injection: Pass dependencies through the container to avoid direct creation within the function, improving flexibility. Service container: stores dependency instances for easy access in the program, further enhancing loose coupling. Practical case: The sample application demonstrates the practical application of dependency injection and service containers, injecting dependencies into the controller, reflecting the advantages of loose coupling.
