5

Lets say I have a controller, which depends on two modules that both contain a directive or a service with the same name. Can I specify which one exactly should be used?

angular.module('myApp', ['secondModule', 'thirdModule'])
    .controller('Ctrl1', ['$scope', 'myService', function(scope, myService){
        scope.user = myService.getUser();   
        console.log(myService); 
}]);

In this case both secondModule and thirdModule have a service called myService. But only the one from the thirdModule will be used in this example. I tried putting something like secondModule.myService as a dependency for Ctrl1, but it wouldn't work. Is there some kind of namespacing in AngularJS?

2
  • you should really avoid this!! at the moment this does not seem possible Commented Feb 21, 2013 at 21:40
  • 2
    This seems like a real issue when developing large scale apps which could have many directives/services/constants etc. Using simple naming conventions isn't a strong enough mechanism imo. Commented Apr 18, 2013 at 12:57

3 Answers 3

2

At the moment, no, there is no module based namespacing. You'll have to avoid collisions on your own.

I believe there was some discussion about this, but I can't find it at the moment.

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

Comments

0

I use the following convention: https://github.com/roytruelove/angular-grunt-coffeescript#modules-and-angular-items

Comments

0

How about namespacing the services (and also controllers)?

angular.module('secondModule', [])
    .factory('secondModule.myService', function($http) {
        return {
            getUser: function() { /* some code here */ }
        };
    });

angular.module('thirdModule', [])
    .factory('thirdModule.myService', function($http) {
        return {
            getGroup: function() { /* some code here */ }
        };
    });

angular.module('myApp', ['secondModule', 'thirdModule'])
    .controller('myApp.Ctrl1',
        ['$scope', 'secondModule.myService', function(scope, myService){
            scope.user = myService.getUser();   
            console.log(myService); 
        }]
    );

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.