Another way you could do this is to add a method to rootScope and then access it via $ scope. $ root in the controllers, thereby avoiding another injection. I do not know how bad this is, how global.
testapp.js
(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); }; }]); })();
test.html
<!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>Enter text and press the log button.</p> Message: <input type="text" ng-model="message"/> <button ng-click="logThis(message)">log</button> </div> </body> </html>
source share