如何区分AngularJS中注入模块的名称空间

How to differentiate between namespaces of injected modules in AngularJS?

本文关键字:空间 模块 注入 何区 AngularJS      更新时间:2023-09-26

假设我有一个控制器,它依赖于两个模块,这两个模块都包含一个同名的指令或服务。我能具体说明应该使用哪一个吗?

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

在这种情况下,secondModulethirdModule都有一个名为myService的服务。但在本例中,仅使用第三模块中的一个。我尝试将类似于secondModule.myService的东西作为Ctrl1依赖项,但它不起作用。AngularJS中是否存在某种命名?

目前,没有基于模块的名称空间。你必须自己避免碰撞。

我相信有人讨论过这个问题,但我现在找不到。

我使用以下约定:https://github.com/roytruelove/angular-grunt-coffeescript#modules-和角度项目

对服务(以及控制器)进行名称调整如何?

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); 
        }]
    );