0

I'm wondering about some AngularJS behaviour. I'm curious if AngularJS modules inherit the dependencies of other modules. Let's say i have this structure:

var PVNServices = angular.module('PVN.services', []);

PVNServices.factory('ItemService', ['$q', 'apiUrl', '$http', 'errorHandler', function($q, apiUrl, $http, errorHandler) {
    return {
        getAlert: function(alert_id, user_id, categorie_id) {
            return $http({  method: 'get',
                            url: apiUrl + 'getAlert/',
                            params : {
                'alert_id' : alert_id,
                'user_id' : user_id,
                'categorie_id' : categorie_id,
              }
            }).then(function(res){return res.result }, errorHandler);
        }
  }
}]);

var PVNControllers = angular.module('PVN.controllers', ['PVN.services']);

PVNControllers.controller('AppController', ['$scope', 'ItemService', function($scope, ItemService) {
  $scope.getAlert = function(alert_id, user_id, categorie_id){
     ItemService.getAlert(alert_id, user_id, categorie_id).then(function(alert){
         $scope.alert = alert;
     }
  }
}]);

var PVNDashboard = angular.module('PVN', ['ngSanitize','ngMaterial','PVN.controllers'], function($interpolateProvider) {
    $interpolateProvider.startSymbol('<<');
    $interpolateProvider.endSymbol('>>');
});

PVNDashboard.run(function() {
    moment.locale('nl');
});

<body class="" ng-app="PVN">
</body>

With this structure, would I be able to use the ItemService in the PVNDashboard module because it has the controllers as dependency which in turn has the services as a dependecy. And because of the ng-app being the PVN module will the configuration of the PVN module, moment.js locale in this example case. Also persist in the services because it's the first to run?

1 Answer 1

1

Yes, the dependencies all inherit. The intention was that you could create a module for each feature and create apps by injecting several modules.

There is one catch: angular seems to use a single namespace for all injectable things, so it will overwrite anything with the same name. See this blog for example. http://michalostruszka.pl/blog/2015/05/21/angular-dependencies-naming-clash/

I'm not sure if anything has changed and some comments say this should be fixed in angular 2.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.