Angularjs:从服务中获取服务名称

Angularjs:从服务中获取服务名称,angularjs,Angularjs,如何从对象中获取服务(或工厂,提供商,控制器,指令…)的名称?乙二醇 angular.module('app', []).controller('myCtrl', function() { // how do I get the name 'myCtrl' here? }) 显而易见的方法是硬编码,但我想避免这种情况 .service('myService', function() { this.name = 'myService'; }); 我想这是一种方法,但我希望避免它,因为我

如何从对象中获取
服务
(或
工厂
提供商
控制器
指令
…)的名称?乙二醇

angular.module('app', []).controller('myCtrl', function() {
  // how do I get the name 'myCtrl' here?
})
显而易见的方法是硬编码,但我想避免这种情况

.service('myService', function() {
  this.name = 'myService';
});
我想这是一种方法,但我希望避免它,因为我不想在我的应用程序中声明全局变量

var myCtrlName = 'myCtrl';
angular.module('app', []).controller(myCtrlName, function() {
  console.log(myCtrlName);
});

我认为,不可能获得服务/工厂名称

如果我们谈论的是控制器名称,则可以通过订阅
$routeChangeSuccess
事件

$scope.$on('$routeChangeSuccess', function () {
     console.log($route.current.controller);
});
另外,如果我们谈论的是指令。你可以使用这个技巧

app.directive('directiveOne', function (){
  return {
    controller: 'SomeCtrl',
    scope: true,
    template: '<button ng-click="myFun()">Test A</button>',
    link: function(scope, elem, attrs, ctrl) {
      ctrl.directiveName = 'directiveOne';
    }
  };
});

app.directive('directiveTwo', function (){
  return {
    controller: 'SomeCtrl',
    scope: true,
    template: '<button ng-click="myFun()">Test B</button>',
    link: function(scope, elem, attrs, ctrl) {
      ctrl.directiveName = 'directiveTwo';
    }
  };
});

app.controller('SomeCtrl', function ($scope){
    var ctrl = this;
    $scope.test = function (){
       console.log('Directive name is: ' + ctrl.directiveName);
    };
});
app.directive('directiveOne',function(){
返回{
控制器:“SomeCtrl”,
范围:正确,
模板:“测试A”,
链接:函数(范围、元素、属性、ctrl){
ctrl.directiveName='directiveOne';
}
};
});
app.directive('directiveTwo',function(){
返回{
控制器:“SomeCtrl”,
范围:正确,
模板:“测试B”,
链接:函数(范围、元素、属性、ctrl){
ctrl.directiveName='directiveTwo';
}
};
});
app.controller('SomeCtrl',函数($scope){
var ctrl=this;
$scope.test=函数(){
log('指令名为:'+ctrl.directiveName);
};
});

如果您使用的是ui路由器,您可以通过$state服务了解控制器的名称

$state.current.controller;

好的,这是怎么做的,但是要注意,它非常脏,而且它使用了angular:D中未记录的东西

var app = angular.module('app', []);
app.controller("Ctrl", function($scope, Serv) {

});
app.service("Serv", function() {
  //We're gonna compare functions, the current one and the one registered in the invokeQueue of the app
  var callee= arguments.callee;
  //Loop the invokeQueue 
  _.each(app._invokeQueue, function(inv) {
    //Check if the functions match
    if(inv[2][1] === callee) {
      //Print the name of the current service!
      console.log(inv[2][0]);
    }
  });
});
检查此jsbin以查看其运行情况:

将服务名称定义为一个变量,以便可以重用,然后封装在闭包中以避免创建全局变量

var app = angular.module('app', []);
(function () {
    var serviceName = "myService";
    app.service(serviceName, function () {
        this.name = serviceName;
    });
})();    

你为什么需要这个?一旦依赖代码中的名称,就会遇到问题。@Yoshi我想用它向中介服务注册回调。我将服务的名称以.init形式传递给中介,以便中介为每个服务都有一个id。Mediator连接端点有点像
$routeProvider.when('/',{templateUrl:'views/main.html',controller:'myCtrl',…})
,它也依赖于名称,在Angular中是一种公认的(确实著名的)模式。你还有其他建议吗?Angulars对名称的使用总是依赖于这样一个事实,即所讨论的元素曾经在该特定名称下注册。这从来不是猜测或类似的,它始终是一种二传者/接受者的关系。我不确定你想做的是否是同一个概念。同样从您对问题的评论来看,这看起来像是一个问题。@Yoshi:D是的,可能是一个X-Y问题。我已经构建了一个成功的“X”,没有太多的摸索(我想!)。我只是想删除硬编码的名称(“Y”)。作为您的指令技巧,始终可以使用相同的方式对服务名称进行硬编码
angular.module('app',[]).service('myService',function(){this.name='myService'})
。这是我试图避免的另一种方法(只是添加到我的问题中!),它太脏了,无法接受作为答案,但对你的创造力来说+1。我希望你能加入我的团队。:)