Created
April 16, 2014 04:48
-
-
Save rnarayana/10808289 to your computer and use it in GitHub Desktop.
AngularJS: Log without injecting $log in all controllers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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