Skip to content

Instantly share code, notes, and snippets.

@rnarayana
Created April 16, 2014 04:48
Show Gist options
  • Select an option

  • Save rnarayana/10808289 to your computer and use it in GitHub Desktop.

Select an option

Save rnarayana/10808289 to your computer and use it in GitHub Desktop.
AngularJS: Log without injecting $log in all controllers
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.5/angular.min.js"></script>
<script src="testapp.js"></script>
</head>
<body ng-app="app">
<div ng-controller="LogCtrl">
<p>Reload this page with open console, enter text and hit the log button...</p>
Message:
<input type="text" ng-model="message"/>
<button ng-click="logThis(message)">log</button>
</div>
</body>
</html>
(function(){
'use strict';
angular.module('app', [])
.run(function($rootScope, $log) {
$rootScope.log = function(msg){
$log.info(msg);
}
})
.controller('LogCtrl', ['$scope', function LogCtrl($scope) {
$scope.logThis = function(msg){
$scope.$root.log(msg);
};
}]);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment