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

Deep dive into modularization and dependency injection in AngularJS

青灯夜游
Release: 2021-09-02 10:17:59
forward
1445 people have browsed it

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!

source:juejin.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!