search
HomeWeb Front-endJS TutorialDeep dive into modularization and dependency injection in AngularJS

Deep dive into modularization and dependency injection in AngularJS

Sep 02, 2021 am 10:17 AM
angularjsdependency injectionModular

Deep dive into modularization and dependency injection in AngularJS

AngularJS Comes with built-in dependency injection mechanism. You can split your application into multiple components of different types that AngularJS can inject into each. Modularizing your application makes it easier to reuse, configure, and application test components. [Related tutorial recommendations: "angular tutorial"]

AngularJS contains the following core types of objects and components:

  • Value
  • Factory
  • Services
  • Providers
  • persistent

These core types can be injected into each other using the AngularJS dependency injection mechanism. Throughout the rest of this article, I will explain how to define and inject these components into each other.

Value

A value in AngularJS is a simple object. It can be a number, string or JavaScript object. Values ​​are typically used as configuration to inject into factories, services, or controllers.

A value must belong to an AngularJS module. Here are three examples of values ​​added to AngularJS modules:

var myModule = angular.module("myModule", []);

myModule.value( “numberValue”,999);

myModule.value( “stringValue的”, “ABC”);

myModule.value("objectValue", { val1 : 123, val2 : "abc"} );

The values ​​are defined using the value() function on the module. The first parameter is the name of the value, and the second parameter is the value itself. Factories, services and controllers can now reference these values ​​by their names.

Inject the value

The value injected into the AngularJS controller function is by passing the parameter Complete with the same name as the value (passing the first argument to the value() function is simply the value when it is defined). Here is an example:

var myModule = angular.module("myModule", []);

myModule.value( “ numberValue ”,999);

myModule.controller( “myController的”功能($范围,numberValue){

    的console.log(numberValue);

});

Notify the controller how the second parameter of the function has the same name as the value.

Factory

Factory is a function that creates value. When a service, controller, etc. requires a value injected from the factory, the factory creates the value on demand. Once created, this value will be reused for all services, controllers, etc. that need to be injected. So a factory differs from a value in that it can use a factory function to create the object it returns. You can also inject values ​​into the factory to use when creating objects. You can't do this with a value.

Here's an example of defining a factory on a module, and a controller that gets the factory created value injected:

var myModule = angular.module("myModule", []);
myModule.factory("myFactory", function() {
return "a value";
});

myModule.controller("MyController", function($scope, myFactory) {
console.log(myFactory);
});

As you can see, it's very similar to defining and injecting a value object. Remember, it is not the factory function that is injected, but the value produced by the factory function.

Injecting values ​​into factories

You can inject values ​​into factories. It works just like injecting values ​​into the controller. Here is an example:

var myModule = angular.module("myModule", []);
myModule.value("numberValue", 999);
myModule.factory("myFactory", function(numberValue) {
return "a value: " + numberValue;
});

In this example, the injected value is used to create the object created by the factory function.

Services

A service in AngularJS is a singleton JavaScript object that contains a set of functions. These functions contain any logic the service needs to do its job.

AngularJS services are created using the service() function on the module. Here's an example:

function MyService() {this.doIt = function() {console.log("done");}}
var myModule = angular.module("myModule", []);
myModule.service("myService", MyService);

As you can see, the definition of a service is different with factories and values. First, the service is defined as a single named function. This is because services in AngularJS are created using the new keyword. So AngularJS will do this internally:

var theService = new MyService();

In addition to defining services as functions with functions inside, you can also add them to and use them with AngularJS, like Same as using values ​​or functions. You can inject the service into the controller like this:

function MyService() {
    this.doIt = function() {
        console.log("done");
    }
}


var myModule = angular.module("myModule", []);

myModule.service("myService", MyService);


myModule.controller("MyController", function($scope, myService) {

    myService.doIt();

});

Value injection into the service

You can inject values ​​into services, just like you can inject values ​​into controllers, or services into controllers, etc. Here is an example:

var myModule = angular.module("myModule", []);

myModule.value  ("myValue"  , "12345");

function MyService(myValue) {
    this.doIt = function() {
        console.log("done: " + myValue;
    }
}

myModule.service("myService", MyService);

Notice how the MyService function's parameters are named the same as the values ​​registered on the module. Therefore, this value will be injected into the service on creation.

provider

AngularJS 中的提供者是您可以创建的最灵活的工厂形式。您可以像使用服务或工厂一样使用模块注册提供者,但您要使用该 provider()函数。这是一个 AngularJS 提供程序示例:

var myModule = angular.module("myModule", []);

myModule.provider("mySecondService", function() {
    var provider = {};

    provider.$get = function() {
        var service = {};

        service.doService = function() {
            console.log("mySecondService: Service Done!");
        }

        return service;
    }

    return provider;
});

如您所见,该provider()函数采用 2 个参数。第一个参数是提供者创建的服务/对象的名称。在这种情况下,名称是mySecondService。第二个参数是创建提供程序的函数。注意:提供者本身是一个工厂,因此此时没有从提供者创建实际的服务或对象。仅定义了创建提供程序的函数。

当您查看创建提供程序的函数时,您可以看到提供程序是一个 JavaScript 对象。

JavaScript 提供程序对象包含一个$get()函数。这是提供者的工厂函数。换句话说,该$get()函数会创建提供者创建的任何内容(服务、值等)。在上面的例子中,提供者创建了一个服务对象,其中包含一个名为 的服务函数(标准 JavaScript 函数)doService()

为了将提供者的产品注入控制器,请指定对提供者的依赖,就像使用服务一样。注入控制器的是提供者创建的产品,而不是提供者本身。这是一个 AngularJS 提供程序注入示例:

myModule.controller("MyController", function($scope, mySecondService ) {

    $scope.whenButtonClicked = function() {
        mySecondService.doIt();
    }

});

如您所见,提供者的名称用作控制器函数中的参数。提供者的$get()函数创建的对象将被注入到这个参数中。

配置提供商

可以在模块的配置阶段通过调用其函数来进一步配置提供者。下面是一个例子:

var myModule = angular.module("myModule", []);

myModule.provider("mySecondService", function() {
    var provider = {};
    var config   = { configParam : "default" };

    provider.doConfig = function(configParam) {
        config.configParam = configParam;
    }

    provider.$get = function() {
        var service = {};

        service.doService = function() {
            console.log("mySecondService: " + config.configParam);
        }

        return service;
    }

    return provider;
});

myModule.config( function( mySecondServiceProvider ) {
    mySecondServiceProvider.doConfig("new config param");
});

myModule.controller("MyController", function($scope, mySecondService) {

    $scope.whenButtonClicked = function() {
        mySecondService.doIt();
    }

});

注意 provider 对象现在如何有一个名为 的额外函数doConfig()。此函数可用于在提供程序上设置配置参数。

还要注意对myModule.config()函数的调用。该config函数接受一个函数作为参数。该函数可以配置模块。传递给的函数 config()采用名为 的单个参数mySecondServiceProvider。这与提供者注册的名称相同Provider,后缀为 plus 。后缀告诉 AngularJS 注入提供者本身,而不是提供者创建的对象。在传递给config()函数的mySecondServiceProvider.doConfig()函数内部调用该函数,该函数在提供程序上设置 config 参数。

示例中稍后定义的控制器仅依赖于提供者创建的对象(而不是提供者本身)。它通过获取一个名为mySecondServicename 的参数来实现,该参数是服务提供者注册的名称。如您所见,从$scope.whenButtonClicked()函数内部使用的服务 。

常数

在上一节有关提供程序的部分中,您看到了如何通过module.config() 函数配置提供程序。不幸的是,您不能将值注入到module.config()函数中。相反,您可以注入常量。

AngularJS 中的常量是使用module.constants()函数定义的。这是一个 AngularJS 常量示例:

myModule.constant("configValue", "constant config value");

这个常量现在可以module.config()像这样注入到函数中:

myservices.config( function( mySecondServiceProvider, configValue ) {
    mySecondServiceProvider.doConfig(configValue);
});

如您所见,该参数configValue与常量的名称相匹配,该名称也是 configValue. 因此常量的值将被注入到这个参数中。然后将常量值作为参数传递给提供程序doConfig()上的函数 mySecondServiceProvider

模块之间的依赖关系

如您所见,值、工厂和服务被添加到 AngularJS 模块中。一个模块可以使用另一个模块的值、工厂和服务。为此,模块需要声明对包含它要使用的值、工厂和服务的模块的依赖关系。下面是一个例子:

var myUtilModule = angular.module("myUtilModule", []);

myUtilModule.value("myValue", "12345");


var myOtherModule = angular.module("myOtherModule", [ 'myUtilModule' ]);

myOtherModule.controller("MyController", function($scope, myValue ) {

});

注意第二个模块 ( myOtherModule)如何在myUtilModule传递给angular.module()函数的第二个参数(数组内部)中列出第一个模块 ( )的名称。这告诉 AngularJS 中定义的所有值、工厂和服务也myUtilModule应该在myOtherModule模块中可用。换句话说,myOtherModule 取决于 myUtilModule

其次,注意MyController控制器函数现在如何声明一个名为 的参数myValue。该值将从在myUtilModule模块上注册的值提供。

微小的安全依赖注入AngularJS

当您缩小 JavaScript 时,JavaScript minifier 会用较短的名称替换局部变量和参数的名称。然而,AngularJS 使用控制器函数、工厂、服务和提供者的参数名称来决定将什么注入到他们的工厂函数中。如果名称更改,AngularJS 将无法注入正确的对象。

为了使您的 AngularJS 代码压缩安全,您需要提供要作为字符串注入的对象的名称。您将这些字符串与需要注入值的函数一起包装在一个数组中。这是一个 AngularJS 压缩安全依赖注入示例:

var myapp = angular.module("myapp", ['myservices']);

myapp.controller("AController", ['$scope', function(p1) {
    p1.myvar = "the value";
}]);

本示例将$scope对象注入到p1控制器函数的参数中。

注意控制器函数是如何注册的。不是angular.controller直接将控制器函数传递给函数,而是传递一个数组。该数组包含要注入控制器函数的值的名称,以及控制器函数本身。控制器函数始终是该数组中的最后一个值。如果需要注入多个值,则值名称列在数组的开头,并按它们要注入函数的顺序列出。这是一个缩小安全的多值示例:

var myapp = angular.module("myapp", ['myservices']);

myapp.controller("AController", ['$scope', '$http', function(p1, p2) {
    p1.myvar = "the value";
    p2.get("/myservice.json");
}]);

此示例将$scope对象注入p1参数,并将$http服务注入p2控制器函数的参数。

现在,控制器函数的参数名称不再重要。AngularJS 将使用数组开头的字符串来确定要注入到控制器函数中的内容。

相同的机制可用于工厂、服务和提供者,以提供缩小安全的依赖注入。这是一个小型化安全工厂、服务和提供程序示例:

var myutil = angular.module("myutil", []);


myutil.value("safeValue", "a safe value");


myutil.factory("safeFactory", ['safeValue', function(p1) {
    return { value : p1 };
}]);


function MySafeService(p1){
    this.doIt = function() {
        return "MySafeService.doIt() called: " + p1.value;
    }
}
myutil.service("safeService", ['safeFactory', MySafeService]);


myutil.provider("safeService2", function() {
    var provider = {};

    provider.$get = ['safeService', function(p1) {
        var service = {};

        service.doService = function() {
            console.log("safeService from provider: " + p1.doIt());
        }

        return service;
    }];

    return provider;
});

myapp.controller("AController", ['$scope', 'safeService2', function(p1, p2) {
    p1.myvar = "the value";
    p2.doService();
}]);

尤其要注意的provider的声明。注意,此时的依赖不会对provider工厂函数指定,但对$get()从provider工厂函数内部返回的供应商的功能。实际上,使用具有依赖项名称和函数实现的数组而不仅仅是$get() 函数。

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of Deep dive into modularization and dependency injection in AngularJS. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!