Table of Contents
Value
Factory
Services
provider
常数
模块之间的依赖关系
微小的安全依赖注入AngularJS
Home Web Front-end JS Tutorial Deep dive into modularization and dependency injection in AngularJS

Deep dive into modularization and dependency injection in AngularJS

Sep 02, 2021 am 10:17 AM
angularjs dependency injection Modular

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"} );
Copy after login

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);

});
Copy after login

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);
});
Copy after login

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;
});
Copy after login

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);
Copy after login

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();
Copy after login

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();

});
Copy after login

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);
Copy after login

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;
});
Copy after login

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

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

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

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

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

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

});
Copy after login

如您所见,提供者的名称用作控制器函数中的参数。提供者的$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();
    }

});
Copy after login

注意 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");
Copy after login

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

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

如您所见,该参数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 ) {

});
Copy after login

注意第二个模块 ( 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";
}]);
Copy after login

本示例将$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");
}]);
Copy after login

此示例将$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();
}]);
Copy after login

尤其要注意的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 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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to Optimize the Maintainability of Java Code: Experience and Advice How to Optimize the Maintainability of Java Code: Experience and Advice Nov 22, 2023 pm 05:18 PM

How to Optimize the Maintainability of Java Code: Experience and Advice In the software development process, writing code with good maintainability is crucial. Maintainability means that code can be easily understood, modified, and extended without causing unexpected problems or additional effort. For Java developers, how to optimize the maintainability of code is an important issue. This article will share some experiences and suggestions to help Java developers improve the maintainability of their code. Following standardized naming rules can make the code more readable.

How to solve the code complexity error in Python code? How to solve the code complexity error in Python code? Jun 24, 2023 pm 05:43 PM

Python is a simple, easy-to-learn and efficient programming language, but when we write Python code, we may encounter some problems with excessive code complexity. If these problems are not solved, it will make the code difficult to maintain, error-prone, and reduce the readability and scalability of the code. So, in this article, we will discuss how to resolve code complexity error in Python code. Understanding Code Complexity Code complexity is a measure of the nature of code that is difficult to understand and maintain. In Python, there are some indicators that can be used

How to solve the poor maintainability error of Python code? How to solve the poor maintainability error of Python code? Jun 25, 2023 am 11:58 AM

Python, as a high-level programming language, is widely used in software development. Although Python has many advantages, a problem that many Python programmers often face is that the maintainability of the code is poor. The maintainability of Python code includes the legibility, scalability, and reusability of the code. In this article, we will focus on how to solve the problem of poor maintainability of Python code. 1. Code readability Code readability refers to the readability of the code, which is the core of code maintainability.

How to use dependency injection (Dependency Injection) in the Phalcon framework How to use dependency injection (Dependency Injection) in the Phalcon framework Jul 30, 2023 pm 09:03 PM

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

Dependency injection pattern in Golang function parameter passing Dependency injection pattern in Golang function parameter passing Apr 14, 2024 am 10:15 AM

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.

Guide to implementing modular development in Vue large-scale projects Guide to implementing modular development in Vue large-scale projects Jun 09, 2023 pm 04:07 PM

In modern web development, Vue, as a flexible, easy-to-use and powerful front-end framework, is widely used in the development of various websites and applications. When developing large-scale projects, how to simplify the complexity of the code and make the project easier to maintain is a problem that every developer must face. Modular development can help us better organize code, improve development efficiency and code readability. Below, I will share some experiences and guidelines for implementing modular development in Vue large-scale projects: 1. Clear division of labor in a large-scale project

Dependency injection using JUnit unit testing framework Dependency injection using JUnit unit testing framework Apr 19, 2024 am 08:42 AM

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.

Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Jun 27, 2023 pm 07:37 PM

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

See all articles