poshest
poshest

Reputation: 4237

Angularjs: get service name from within service

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

Upvotes: 2

Views: 2571

Answers (4)

haimlit
haimlit

Reputation: 2582

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

Upvotes: 2

Eric Nijman
Eric Nijman

Reputation: 11

Ok, this is how to do it, but be aware, it's very dirty and it uses undocumented stuff from 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]);
    }
  });
});

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

Upvotes: 1

cubbuk
cubbuk

Reputation: 7920

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

$state.current.controller;

Upvotes: 0

Artyom Pranovich
Artyom Pranovich

Reputation: 6962

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.

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

Upvotes: 0

Related Questions