How to distinguish namespaces of embedded modules in AngularJS?

Suppose I have a controller that depends on two modules that contain a directive or service with the same name. Can I indicate which one to use?

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 named myService . But in this example, only one of the thirdModule will be used . I tried putting something like secondModule.myService as a dependency for Ctrl1 , but this will not work. Is there any namespace in AngularJS?

+5
source share
3 answers

, . .

, - , .

+2

What about placing service names (as well as 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); 
        }]
    );
0
source

All Articles