Detailed description of the dependency injection mechanism in AngularJS
This article mainly introduces the dependency injection mechanism in AngularJS, which plays a very important role in the use of JavaScript components. For more information, see
Dependency injection is a software design pattern given in the component that replaces the hard components to encode their dependencies. This reduces the dependency and dependency configuration of a component. This helps make components reusable, maintained, and tested.
AngularJS provides a supreme dependency injection mechanism. It provides a core component that can be injected and dependent on each other.
Value
Factory
Service
Provider
Constant Value
Value
A value is a simple JavaScript Object used to configure the Phase Controller during value transfer.
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Define a module Var mainApp = angular. module ("mainApp", []); // Create a value object as "defaultInput" and pass it a data. MainApp. value ("defaultInput", 5 ); ... // Inject the value in the controller using its name "defaultInput" MainApp. controller ('calccontroller', function ($ scope, CalcService, defaultInput ){ $ Scope. number = defaultInput; $ Scope. result = CalcService. square ($ scope. number ); $ Scope. square = function (){ $ Scope. result = CalcService. square ($ scope. number ); } }); |
Factory
The factory is the value used to return the function. It creates values based on requirements whenever a service or controller needs them. It usually uses a factory function to calculate and return the corresponding value.
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Define a module Var mainApp = angular. module ("mainApp", []); // Create a factory "MathService" which provides a method multiply to return multiplication of two numbers MainApp. factory ('mathservice', function (){ Var factory = {}; Factory. multiply = function (a, B ){ Return a * B } Return factory; }); // Inject the factory "MathService" in a service to utilize the multiply method of factory. MainApp. service ('calcservice', function (MathService ){ This. square = function (){ Return MathService. multiply (a, ); } }); ... |
Service
A service is a single JavaScript that contains a set of function objects to execute certain tasks. The service uses the service () function and injects it into the definition of the controller.
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Define a module Var mainApp = angular. module ("mainApp", []); ... // Create a service which defines a method square to return square of a number. MainApp. service ('calcservice', function (MathService ){ This. square = function (){ Return MathService. multiply (a, ); } }); // Inject the service "CalcService" into the controller MainApp. controller ('calccontroller', function ($ scope, CalcService, defaultInput ){ $ Scope. number = defaultInput; $ Scope. result = CalcService. square ($ scope. number ); $ Scope. square = function (){ $ Scope. result = CalcService. square ($ scope. number ); } }); |
Provider
Services and factories in the configuration phase during the internal creation of AngularJS used by the provider (phase during which AngularJS directs itself ). The script mentioned below can be used for creation. We have already created MathService. A provider is a special factory method and get () method used to return values, services, and factories.
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Define a module Var mainApp = angular. module ("mainApp", []); ... // Create a service using provider which defines a method square to return square of a number. MainApp. config (function ($ provide ){ $ Provide. provider ('mathservice', function (){ This. $ get = function (){ Var factory = {}; Factory. multiply = function (a, B ){ Return a * B; } Return factory; }; }); }); |
Constant
Constants are used to consider facts by configuring the phase value. values cannot be transferred during the configuration phase.
?
| 1 |
MainApp. constant ("configParam", "constant value "); |
Example
The following example shows all the preceding commands.
TestAngularJS.html
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<Html> <Head> <Title> AngularJS Dependency Injection </title> </Head> <Body> <H2> AngularJS Sample Application <Div ng-app = "mainApp" ng-controller = "CalcController"> <P> Enter a number: <input type = "number" ng-model = "number"/> <Button ng-click = "square ()"> X <sup> 2 </sup> </button> <P> Result: {result }}</p> </Div> <Script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"> </script> <Script> Var mainApp = angular. module ("mainApp", []); MainApp. config (function ($ provide ){ $ Provide. provider ('mathservice', function (){ This. $ get = function (){ Var factory = {}; Factory. multiply = function (a, B ){ Return a * B; } Return factory; }; }); }); MainApp. value ("defaultInput", 5 ); MainApp. factory ('mathservice', function (){ Var factory = {}; Factory. multiply = function (a, B ){ Return a * B; } Return factory; }); MainApp. service ('calcservice', function (MathService ){ This. square = function (){ Return MathService. multiply (a, ); } }); MainApp. controller ('calccontroller', function ($ scope, CalcService, defaultInput ){ $ Scope. number = defaultInput; $ Scope. result = CalcService. square ($ scope. number ); $ Scope. square = function (){ $ Scope. result = CalcService. square ($ scope. number ); } }); </Script> </Body> </Html> |
Result
Open textangularjs.html in the webbrowser. The result is as follows.