Angular allows the service to declare other service as dependent, using the constructor used when instantiating itself.
In order to claim dependencies, we need to specify them in the factory method declaration, and in the factory method by the $inject property (string identifier array) or by using the array notation.
Usually the $inject attribute declaration can be discarded (that is, the implicit dependency injection referred to in http://www.jb51.net/article/91815.htm, but this is an experimental property, and it will fail after the compression is confused.) )。
Using the array notation
function Mymodulecfgfn ($provide) {
$provide. Factory (' MyService ', [' dep1 ', ' Dep2 ', function (DEP1,DEP2) {}]);
}
Using the $inject property
function Mymodulecfgfn ($provide) {
var myservicefactory = function (Dep1, DEP2) {};
Myservicefactory. $inject = [' Dep1 ', ' Dep2 '];
$provide. Factory (' MyService ', myservicefactory);
}
Using implicit di (code that is not compatible with compression obfuscation)
function Mymodulecfgfn ($provide) {
$provide. Factory (' MyService ', function (DEP1, DEP2) {});
Below is an example of two service, dependencies between them, and other service provided by angular.
The/** * Batchlog service allows messages to form queues in memory, which is flush once in 50 seconds.
* * @param {*} message logged.
* * Function Batchlogmodule ($provide) {$provide. Factory (' Batchlog ', [' $timeout ', ' $log ', function ($timeout, $log) {
var messageQueue = [];
function log () {if (messagequeue.length) {$log (' Batchlog messages: ', messageQueue);
MessageQueue = [];
} $timeout (log, 50000);
} log ();
return function (message) {messagequeue.push (message);
} }]); /** * Routetemplatemonitor monitoring every route change, each than the slave ah will be recorded through the Batchlog service/$provide. Factory (' Routetemplatemonitor ' , [' $route ', ' batchlog ', ' $rootScope ', function ($route, Batchlog, $rootScope) {$rootScope. $on (' $routeCha
Ngesuccess ', function () {Batchlog ($route. Current $route. Current.template:null);
});
}]);
//Get the main service, run the Application (Listener event) Angular.injector ([Batchlogmodule]). Getting (' routetemplatemonitor ');
Examples of things to note:
- The Batchlog service relies on angular built-in $timeout (http://docs.angularjs.org/api/ng. $timeout) and $log services (http:// Docs.angularjs.org/api/ng. $log), implemented through Console.log bulk log messages.
- The Routetemplatemonitor service relies on the built-in $route (http://docs.angularjs.org/api/ng. $route) service with our custom Batchlog service.
- All two of our service uses the factory method signature and array notation to annotate inject, declaring their dependencies. It is important that the order of the string identities in the array be consistent with the order in the factory method signature (parameters). Unless an implicit dependency declaration is used in a factory method parameter, injector determines which service to inject based on the order of the strings in the array.
The above is about ANGULARJS managing Service dependencies data collation, follow-up continue to add relevant information, thank you for your support of this site!