ANGULARJS managing Service Dependencies Detailed _angularjs

Source: Internet
Author: User

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:

    1. 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.
    2. The Routetemplatemonitor service relies on the built-in $route (http://docs.angularjs.org/api/ng. $route) service with our custom Batchlog service.
    3. 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.