从服务内部获取服务名

时间:2022-08-14 18:12:15

How can I get the name of a service (or factory, provider, controller, directive...) from WITHIN the object? Eg

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

angular.module('app', []).controller('myCtrl', function() {
  // how do I get the name 'myCtrl' here?
})

The obvious way is hard coding, but I want to avoid that

显而易见的方法是硬编码,但我想避免这种情况。

.service('myService', function() {
  this.name = 'myService';
});

I guess this is a way, but I'm hoping to avoid it as I don't want to have to declare globals in my app

我想这是一种方法,但我希望避免它,因为我不想在我的应用中声明全局变量

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

4 个解决方案

#1


2  

Define your service name as a variable so you can reuse, then wrap in a closure to avoid creating a global variable.

定义您的服务名称作为变量,以便您可以重用,然后在闭包中包装以避免创建全局变量。

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

#2


1  

Ok, this is how to do it, but be aware, it's very dirty and it uses undocumented stuff from angular :D

好的,这就是怎么做的,但是要注意,它很脏而且它使用了角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]);
    }
  });
});

Check this jsbin to see it in action: http://jsbin.com/yugogonoya/edit?html,js,console,output

检查这个jsbin,查看它的运行情况:http://jsbin.com/南斯拉夫/edit?html、js、控制台、输出

#3


0  

I think, that it's not possible to get service/factory name.

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

If we are talking about controller name, it's possible with subscription on $routeChangeSuccess event.

如果我们讨论的是控制器名,可以订阅$routeChangeSuccess事件。

$scope.$on('$routeChangeSuccess', function () {
     console.log($route.current.controller);
});

Also, if we are talking about directive. You can use this trick.

同样,如果我们说的是指令。你可以用这个技巧。

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

#4


0  

If you are using ui-router you can learn the name of the controller through $state service.

如果您正在使用ui-router,您可以通过$state服务了解控制器的名称。

$state.current.controller;

#1


2  

Define your service name as a variable so you can reuse, then wrap in a closure to avoid creating a global variable.

定义您的服务名称作为变量,以便您可以重用,然后在闭包中包装以避免创建全局变量。

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

#2


1  

Ok, this is how to do it, but be aware, it's very dirty and it uses undocumented stuff from angular :D

好的,这就是怎么做的,但是要注意,它很脏而且它使用了角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]);
    }
  });
});

Check this jsbin to see it in action: http://jsbin.com/yugogonoya/edit?html,js,console,output

检查这个jsbin,查看它的运行情况:http://jsbin.com/南斯拉夫/edit?html、js、控制台、输出

#3


0  

I think, that it's not possible to get service/factory name.

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

If we are talking about controller name, it's possible with subscription on $routeChangeSuccess event.

如果我们讨论的是控制器名,可以订阅$routeChangeSuccess事件。

$scope.$on('$routeChangeSuccess', function () {
     console.log($route.current.controller);
});

Also, if we are talking about directive. You can use this trick.

同样,如果我们说的是指令。你可以用这个技巧。

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

#4


0  

If you are using ui-router you can learn the name of the controller through $state service.

如果您正在使用ui-router,您可以通过$state服务了解控制器的名称。

$state.current.controller;