1

I want to log the usage of different features in our Angularjs application. Current feature in use is usually identified by the current route/url in browser but there are some features which open in a modal/popup or with ngInclude without change in route/url.

Is there any central way to identify which feature is in use currently, i thought of getting the current top level controller in use but unfortunately don't know how to get it.

Any other idea for that purpose?

P.S. I am using ngRoute.

Mistalis
  • 16,351
  • 13
  • 68
  • 91
arsinawaz
  • 470
  • 1
  • 5
  • 18
  • Could you check if [**my answer**](https://stackoverflow.com/a/44154164/4927984) solved your issue? :) – Mistalis May 26 '17 at 09:55
  • @Mistalis sorry it didn't. I was looking for an application wide solution, and your solution doesn't cover all aspects. :( – arsinawaz May 26 '17 at 10:24

3 Answers3

2

I see several way to log infos from/of the controller.

1) using element.controller()

From the docs of element.controller():

controller(name) - retrieves the controller of the current element or its parent. By default retrieves controller associated with the ngController directive. If name is provided as camelCase directive name, then the controller for this directive will be retrieved (e.g. 'ngModel').

You can this function on very element to get its parent controller. Here is an snippet example using a directive:

var app = angular.module('app', [])
app.controller('MainController', function MainController() {});
app.controller('directiveCtrl', function directiveCtrl() {});

app.directive('controllerName', function($timeout) {
    return {
      restrict: 'A',
      template: '<div>My controller name is: {{cName}}</div>',
      controller: 'directiveCtrl',
      link: function($scope, elem, attrs) {
        var name;
        name = elem.controller().constructor.name;
        $scope.cName = name;
      }
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
<div ng-app="app">
  <div ng-controller="MainController">
    <div controller-name></div>
  </div>
  ***************************************************
  <div ng-controller="MainController">
    <div ng-controller="directiveCtrl">
      <div controller-name></div>
    </div>
  </div>
  <hr/>
</div>

2) using the console

You can access to every Angular object from your web console. For example, typing angular.element($0).scope() will get the current scope.

See this answer, which is very complete about the use of the console.

3) logging from the controller

You can add a console call in your controller at the beginning of the code, and log whatever you want. This way, you will know each time a controller is instanciated:

app.controller('MyCtrl', function($scope) {
    $scope.greetings = 'Hello world';
    console.log('MyCtrl instanciated!', $scope.greetings);
});
Mistalis
  • 16,351
  • 13
  • 68
  • 91
1

Here is an interesting and funny way of logging instantiated controllers

angular.module('yourModule')
.decorator('$controller', ['$delegate', function controllerDecorator($delegate) {
    return function loggingController (expression, locals, later, ident) {

        // Do all your logging here
        if (typeof (expression) === 'function') {
            console.log('Controller:' + expression.name);

        } else if (locals.$$controller) {
            console.log('Controller: ' + locals.$$controller);

        } else if (expression instanceof Array) {
            console.log('Controller:' + expression[expression.length - 1].name);

        } else {
            console.log('Controller:' + expression);
        }

        // fire regular controller function
        return $delegate(expression, locals, later, ident);
    };
}]);

What you are doing here is pretty much extending angulars controller function so you get certain functionality on all controllers, not only yours

chrystian
  • 691
  • 5
  • 15
  • Thanks, how can i use this as an injectable resource inside a factory to get the current controller name? – arsinawaz May 24 '17 at 09:50
  • you just drop this piece of code somewhere in your app, its not something that you inject somewhere... but remember to **adjust module name** – chrystian May 24 '17 at 09:53
  • if you managed to put it in use, please mark my answer as correct one, thanks! – chrystian Jun 08 '17 at 12:59
-1

Another way is injecting the $route service and calling:

$route.current.controller