Reputation: 42210
I am investigating logging options and conventions in AngularJS. I would like my $log
output to contain both the module
and controller
names (similar to loggers in Log4j). With the code below, I was able to inject the controller
name into the $scope
var app = angular.module("demo", []);
app.config(['$provide', function ($provide) {
$provide.decorator('$controller', [
'$delegate',
function ($delegate) {
return function(constructor, locals) {
if (angular.isString(constructor)) {
locals.$scope.controllerName = constructor;
}
return $delegate(constructor, locals);
}
}]);
}]);
app.controller('SampleCtrl', ['$scope', '$log', function ($scope, $log) {
$log.log("[" + app.name + "." + $scope.controllerName +"] got here");
}]);
I would like to solve this a better way. Specifically:
controller
name that adding it to the $scope
?module
name instead of fetching "demo" from the .name
property of the module app
?$log.log()
calls? Upvotes: 4
Views: 456