AngularJS - Clean way to have many service dependencies without calling themA number of services registering in another service that has no dependencies to them? (lazy-loading)AngularJS : The correct way of binding to a service propertiesAngularJS testing with $httpBackend - Service is not calledAngularJS : Factory and Service?AngularJS: Defining module dependency at run-time, dynamicallyAngularjs - how to correct inject service from another module that is not depending?Angular debug function to show use of injected items$injector:nomod Module 'app' is not available! Firefox OnlyHow to test an AngularJS service which depends on a previous test passing?How could I add dependencies in for module after declaration some where in page

Short story with a alien planet, government officials must wear exploding medallions

How much of data wrangling is a data scientist's job?

How to show a landlord what we have in savings?

How could indestructible materials be used in power generation?

Intersection Puzzle

Do UK voters know if their MP will be the Speaker of the House?

Im going to France and my passport expires June 19th

What about the virus in 12 Monkeys?

Expand and Contract

Can we compute the area of a quadrilateral with one right angle when we only know the lengths of any three sides?

Why can't we play rap on piano?

Why didn't Boeing produce its own regional jet?

Can my sorcerer use a spellbook only to collect spells and scribe scrolls, not cast?

What does the expression "A Mann!" means

Why was the shrinking from 8″ made only to 5.25″ and not smaller (4″ or less)?

How do I handle a potential work/personal life conflict as the manager of one of my friends?

One verb to replace 'be a member of' a club

Personal Teleportation: From Rags to Riches

Is it acceptable for a professor to tell male students to not think that they are smarter than female students?

iPad being using in wall mount battery swollen

Alternative to sending password over mail?

Why would the Red Woman birth a shadow if she worshipped the Lord of the Light?

How do conventional missiles fly?

Am I breaking OOP practice with this architecture?



AngularJS - Clean way to have many service dependencies without calling them


A number of services registering in another service that has no dependencies to them? (lazy-loading)AngularJS : The correct way of binding to a service propertiesAngularJS testing with $httpBackend - Service is not calledAngularJS : Factory and Service?AngularJS: Defining module dependency at run-time, dynamicallyAngularjs - how to correct inject service from another module that is not depending?Angular debug function to show use of injected items$injector:nomod Module 'app' is not available! Firefox OnlyHow to test an AngularJS service which depends on a previous test passing?How could I add dependencies in for module after declaration some where in page













0















I have a service that ("A") needs to be loaded after ~10 other services are loaded ("B" - "L"), because those other 10 each make a call on their startup, and "A" needs those each to be called.



What I know I can do is



var module = angular.module('AModule',[]); 

module.service('A', ['B', 'C', 'D',...'L', function()
//A's functionality

]);


However, within A I never explicitly call any of the other service's methods, and I've never seen a pattern where a service is declared with dependencies on other services, where those services aren't actually injected/used. Is there a better way to do this?



What I tried to do was have



var module = angular.module('AModule',[
BThroughLModule
]);


Where BThroughLModule registers the B through L services. But that didn't work - declaring the module dependency didn't guarantee the services of those modules were instantiated before the A module.










share|improve this question






















  • @georgeawg The other services make a call on instantiation. The A service needs them all to have made that call before it instantiates.

    – sherz1
    Mar 8 at 13:20















0















I have a service that ("A") needs to be loaded after ~10 other services are loaded ("B" - "L"), because those other 10 each make a call on their startup, and "A" needs those each to be called.



What I know I can do is



var module = angular.module('AModule',[]); 

module.service('A', ['B', 'C', 'D',...'L', function()
//A's functionality

]);


However, within A I never explicitly call any of the other service's methods, and I've never seen a pattern where a service is declared with dependencies on other services, where those services aren't actually injected/used. Is there a better way to do this?



What I tried to do was have



var module = angular.module('AModule',[
BThroughLModule
]);


Where BThroughLModule registers the B through L services. But that didn't work - declaring the module dependency didn't guarantee the services of those modules were instantiated before the A module.










share|improve this question






















  • @georgeawg The other services make a call on instantiation. The A service needs them all to have made that call before it instantiates.

    – sherz1
    Mar 8 at 13:20













0












0








0








I have a service that ("A") needs to be loaded after ~10 other services are loaded ("B" - "L"), because those other 10 each make a call on their startup, and "A" needs those each to be called.



What I know I can do is



var module = angular.module('AModule',[]); 

module.service('A', ['B', 'C', 'D',...'L', function()
//A's functionality

]);


However, within A I never explicitly call any of the other service's methods, and I've never seen a pattern where a service is declared with dependencies on other services, where those services aren't actually injected/used. Is there a better way to do this?



What I tried to do was have



var module = angular.module('AModule',[
BThroughLModule
]);


Where BThroughLModule registers the B through L services. But that didn't work - declaring the module dependency didn't guarantee the services of those modules were instantiated before the A module.










share|improve this question














I have a service that ("A") needs to be loaded after ~10 other services are loaded ("B" - "L"), because those other 10 each make a call on their startup, and "A" needs those each to be called.



What I know I can do is



var module = angular.module('AModule',[]); 

module.service('A', ['B', 'C', 'D',...'L', function()
//A's functionality

]);


However, within A I never explicitly call any of the other service's methods, and I've never seen a pattern where a service is declared with dependencies on other services, where those services aren't actually injected/used. Is there a better way to do this?



What I tried to do was have



var module = angular.module('AModule',[
BThroughLModule
]);


Where BThroughLModule registers the B through L services. But that didn't work - declaring the module dependency didn't guarantee the services of those modules were instantiated before the A module.







angularjs angularjs-service






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 22:51









sherz1sherz1

122




122












  • @georgeawg The other services make a call on instantiation. The A service needs them all to have made that call before it instantiates.

    – sherz1
    Mar 8 at 13:20

















  • @georgeawg The other services make a call on instantiation. The A service needs them all to have made that call before it instantiates.

    – sherz1
    Mar 8 at 13:20
















@georgeawg The other services make a call on instantiation. The A service needs them all to have made that call before it instantiates.

– sherz1
Mar 8 at 13:20





@georgeawg The other services make a call on instantiation. The A service needs them all to have made that call before it instantiates.

– sherz1
Mar 8 at 13:20












2 Answers
2






active

oldest

votes


















0














You could use a $watchGroup in a controller to check when a isLoaded property is set to true for all your B ... L services, and only when all of them have that property set to true, then you run a function in service A to load it. Please see sample below.






angular
.module('app', [])
.controller('myController', function($scope, A, B, C)
$scope.A = A;
$scope.B = B;
$scope.C = C;

var watchGroup = [
'B.isLoaded',
'C.isLoaded'
];

$scope.$watchGroup(watchGroup, function(watchGroup)
for (var i = 0; i < watchGroup.length; i++)
if (!watchGroup[1])
return;


A.loadService();
);
)
.factory('A', function($timeout)
var service =
isLoaded: false,
loadService: loadService
;

function loadService()
$timeout(function() //simulate api call
service.isLoaded = true;
, 1000)


return service;
)
.factory('B', function($timeout)
var service =
isLoaded: false
;

$timeout(function() //simulate api call
service.isLoaded = true;
, 3000)


return service;
)
.factory('C', function($timeout)
var service =
isLoaded: false
;

$timeout(function() //simulate api call
service.isLoaded = true;
, 5000)

return service;
)

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app" ng-controller="myController">
<div>Service A is loaded (when B and C are loaded): A.isLoaded</div>
<div>Service B is loaded: B.isLoaded</div>
<div>Service C is loaded: C.isLoaded</div>

</div>








share|improve this answer






























    0














    Rather than the individual service's themselves making a call on their instantiation, you can do something as following.



    Expose an init method on all the services. Then, create a service module and inject all the services in module.run() block and call all service init methods. This way it is much cleaner and you do not to keep track of whether the services were loaded or not. You can then add service module as dependency to Main module.



    angular.module('Services.Init', [])
    .run(['A','B','C', 'D', function(A, B, C, D)
    A.init();
    B.init();
    C.init();
    D.init();
    );

    angular.module('Main', ['Services.Init']);





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054075%2fangularjs-clean-way-to-have-many-service-dependencies-without-calling-them%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You could use a $watchGroup in a controller to check when a isLoaded property is set to true for all your B ... L services, and only when all of them have that property set to true, then you run a function in service A to load it. Please see sample below.






      angular
      .module('app', [])
      .controller('myController', function($scope, A, B, C)
      $scope.A = A;
      $scope.B = B;
      $scope.C = C;

      var watchGroup = [
      'B.isLoaded',
      'C.isLoaded'
      ];

      $scope.$watchGroup(watchGroup, function(watchGroup)
      for (var i = 0; i < watchGroup.length; i++)
      if (!watchGroup[1])
      return;


      A.loadService();
      );
      )
      .factory('A', function($timeout)
      var service =
      isLoaded: false,
      loadService: loadService
      ;

      function loadService()
      $timeout(function() //simulate api call
      service.isLoaded = true;
      , 1000)


      return service;
      )
      .factory('B', function($timeout)
      var service =
      isLoaded: false
      ;

      $timeout(function() //simulate api call
      service.isLoaded = true;
      , 3000)


      return service;
      )
      .factory('C', function($timeout)
      var service =
      isLoaded: false
      ;

      $timeout(function() //simulate api call
      service.isLoaded = true;
      , 5000)

      return service;
      )

      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
      <div ng-app="app" ng-controller="myController">
      <div>Service A is loaded (when B and C are loaded): A.isLoaded</div>
      <div>Service B is loaded: B.isLoaded</div>
      <div>Service C is loaded: C.isLoaded</div>

      </div>








      share|improve this answer



























        0














        You could use a $watchGroup in a controller to check when a isLoaded property is set to true for all your B ... L services, and only when all of them have that property set to true, then you run a function in service A to load it. Please see sample below.






        angular
        .module('app', [])
        .controller('myController', function($scope, A, B, C)
        $scope.A = A;
        $scope.B = B;
        $scope.C = C;

        var watchGroup = [
        'B.isLoaded',
        'C.isLoaded'
        ];

        $scope.$watchGroup(watchGroup, function(watchGroup)
        for (var i = 0; i < watchGroup.length; i++)
        if (!watchGroup[1])
        return;


        A.loadService();
        );
        )
        .factory('A', function($timeout)
        var service =
        isLoaded: false,
        loadService: loadService
        ;

        function loadService()
        $timeout(function() //simulate api call
        service.isLoaded = true;
        , 1000)


        return service;
        )
        .factory('B', function($timeout)
        var service =
        isLoaded: false
        ;

        $timeout(function() //simulate api call
        service.isLoaded = true;
        , 3000)


        return service;
        )
        .factory('C', function($timeout)
        var service =
        isLoaded: false
        ;

        $timeout(function() //simulate api call
        service.isLoaded = true;
        , 5000)

        return service;
        )

        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
        <div ng-app="app" ng-controller="myController">
        <div>Service A is loaded (when B and C are loaded): A.isLoaded</div>
        <div>Service B is loaded: B.isLoaded</div>
        <div>Service C is loaded: C.isLoaded</div>

        </div>








        share|improve this answer

























          0












          0








          0







          You could use a $watchGroup in a controller to check when a isLoaded property is set to true for all your B ... L services, and only when all of them have that property set to true, then you run a function in service A to load it. Please see sample below.






          angular
          .module('app', [])
          .controller('myController', function($scope, A, B, C)
          $scope.A = A;
          $scope.B = B;
          $scope.C = C;

          var watchGroup = [
          'B.isLoaded',
          'C.isLoaded'
          ];

          $scope.$watchGroup(watchGroup, function(watchGroup)
          for (var i = 0; i < watchGroup.length; i++)
          if (!watchGroup[1])
          return;


          A.loadService();
          );
          )
          .factory('A', function($timeout)
          var service =
          isLoaded: false,
          loadService: loadService
          ;

          function loadService()
          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 1000)


          return service;
          )
          .factory('B', function($timeout)
          var service =
          isLoaded: false
          ;

          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 3000)


          return service;
          )
          .factory('C', function($timeout)
          var service =
          isLoaded: false
          ;

          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 5000)

          return service;
          )

          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
          <div ng-app="app" ng-controller="myController">
          <div>Service A is loaded (when B and C are loaded): A.isLoaded</div>
          <div>Service B is loaded: B.isLoaded</div>
          <div>Service C is loaded: C.isLoaded</div>

          </div>








          share|improve this answer













          You could use a $watchGroup in a controller to check when a isLoaded property is set to true for all your B ... L services, and only when all of them have that property set to true, then you run a function in service A to load it. Please see sample below.






          angular
          .module('app', [])
          .controller('myController', function($scope, A, B, C)
          $scope.A = A;
          $scope.B = B;
          $scope.C = C;

          var watchGroup = [
          'B.isLoaded',
          'C.isLoaded'
          ];

          $scope.$watchGroup(watchGroup, function(watchGroup)
          for (var i = 0; i < watchGroup.length; i++)
          if (!watchGroup[1])
          return;


          A.loadService();
          );
          )
          .factory('A', function($timeout)
          var service =
          isLoaded: false,
          loadService: loadService
          ;

          function loadService()
          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 1000)


          return service;
          )
          .factory('B', function($timeout)
          var service =
          isLoaded: false
          ;

          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 3000)


          return service;
          )
          .factory('C', function($timeout)
          var service =
          isLoaded: false
          ;

          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 5000)

          return service;
          )

          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
          <div ng-app="app" ng-controller="myController">
          <div>Service A is loaded (when B and C are loaded): A.isLoaded</div>
          <div>Service B is loaded: B.isLoaded</div>
          <div>Service C is loaded: C.isLoaded</div>

          </div>








          angular
          .module('app', [])
          .controller('myController', function($scope, A, B, C)
          $scope.A = A;
          $scope.B = B;
          $scope.C = C;

          var watchGroup = [
          'B.isLoaded',
          'C.isLoaded'
          ];

          $scope.$watchGroup(watchGroup, function(watchGroup)
          for (var i = 0; i < watchGroup.length; i++)
          if (!watchGroup[1])
          return;


          A.loadService();
          );
          )
          .factory('A', function($timeout)
          var service =
          isLoaded: false,
          loadService: loadService
          ;

          function loadService()
          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 1000)


          return service;
          )
          .factory('B', function($timeout)
          var service =
          isLoaded: false
          ;

          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 3000)


          return service;
          )
          .factory('C', function($timeout)
          var service =
          isLoaded: false
          ;

          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 5000)

          return service;
          )

          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
          <div ng-app="app" ng-controller="myController">
          <div>Service A is loaded (when B and C are loaded): A.isLoaded</div>
          <div>Service B is loaded: B.isLoaded</div>
          <div>Service C is loaded: C.isLoaded</div>

          </div>





          angular
          .module('app', [])
          .controller('myController', function($scope, A, B, C)
          $scope.A = A;
          $scope.B = B;
          $scope.C = C;

          var watchGroup = [
          'B.isLoaded',
          'C.isLoaded'
          ];

          $scope.$watchGroup(watchGroup, function(watchGroup)
          for (var i = 0; i < watchGroup.length; i++)
          if (!watchGroup[1])
          return;


          A.loadService();
          );
          )
          .factory('A', function($timeout)
          var service =
          isLoaded: false,
          loadService: loadService
          ;

          function loadService()
          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 1000)


          return service;
          )
          .factory('B', function($timeout)
          var service =
          isLoaded: false
          ;

          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 3000)


          return service;
          )
          .factory('C', function($timeout)
          var service =
          isLoaded: false
          ;

          $timeout(function() //simulate api call
          service.isLoaded = true;
          , 5000)

          return service;
          )

          <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
          <div ng-app="app" ng-controller="myController">
          <div>Service A is loaded (when B and C are loaded): A.isLoaded</div>
          <div>Service B is loaded: B.isLoaded</div>
          <div>Service C is loaded: C.isLoaded</div>

          </div>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 1:13









          UrielzenUrielzen

          343314




          343314























              0














              Rather than the individual service's themselves making a call on their instantiation, you can do something as following.



              Expose an init method on all the services. Then, create a service module and inject all the services in module.run() block and call all service init methods. This way it is much cleaner and you do not to keep track of whether the services were loaded or not. You can then add service module as dependency to Main module.



              angular.module('Services.Init', [])
              .run(['A','B','C', 'D', function(A, B, C, D)
              A.init();
              B.init();
              C.init();
              D.init();
              );

              angular.module('Main', ['Services.Init']);





              share|improve this answer



























                0














                Rather than the individual service's themselves making a call on their instantiation, you can do something as following.



                Expose an init method on all the services. Then, create a service module and inject all the services in module.run() block and call all service init methods. This way it is much cleaner and you do not to keep track of whether the services were loaded or not. You can then add service module as dependency to Main module.



                angular.module('Services.Init', [])
                .run(['A','B','C', 'D', function(A, B, C, D)
                A.init();
                B.init();
                C.init();
                D.init();
                );

                angular.module('Main', ['Services.Init']);





                share|improve this answer

























                  0












                  0








                  0







                  Rather than the individual service's themselves making a call on their instantiation, you can do something as following.



                  Expose an init method on all the services. Then, create a service module and inject all the services in module.run() block and call all service init methods. This way it is much cleaner and you do not to keep track of whether the services were loaded or not. You can then add service module as dependency to Main module.



                  angular.module('Services.Init', [])
                  .run(['A','B','C', 'D', function(A, B, C, D)
                  A.init();
                  B.init();
                  C.init();
                  D.init();
                  );

                  angular.module('Main', ['Services.Init']);





                  share|improve this answer













                  Rather than the individual service's themselves making a call on their instantiation, you can do something as following.



                  Expose an init method on all the services. Then, create a service module and inject all the services in module.run() block and call all service init methods. This way it is much cleaner and you do not to keep track of whether the services were loaded or not. You can then add service module as dependency to Main module.



                  angular.module('Services.Init', [])
                  .run(['A','B','C', 'D', function(A, B, C, D)
                  A.init();
                  B.init();
                  C.init();
                  D.init();
                  );

                  angular.module('Main', ['Services.Init']);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 1:22









                  Srinivas PailaSrinivas Paila

                  817610




                  817610



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054075%2fangularjs-clean-way-to-have-many-service-dependencies-without-calling-them%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      1928 у кіно

                      Захаров Федір Захарович

                      Ель Греко