diff options
Diffstat (limited to 'docs/content/guide/dev_guide.services.managing_dependencies.ngdoc')
| -rw-r--r-- | docs/content/guide/dev_guide.services.managing_dependencies.ngdoc | 71 |
1 files changed, 36 insertions, 35 deletions
diff --git a/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc b/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc index b3135b83..5ca76654 100644 --- a/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc +++ b/docs/content/guide/dev_guide.services.managing_dependencies.ngdoc @@ -14,40 +14,46 @@ provided by angular's web framework: <pre> /** -* batchLog service allows for messages to be queued in memory and flushed -* to the console.log every 50 seconds. -* -* @param {*} message Message to be logged. -*/ -angular.module.ng('batchLog', function($defer, $log) { - var messageQueue = []; - - function log() { - if (messageQueue.length) { - $log('batchLog messages: ', messageQueue); - messageQueue = []; - } - $defer(log, 50000); - } + * batchLog service allows for messages to be queued in memory and flushed + * to the console.log every 50 seconds. + * + * @param {*} message Message to be logged. + */ + function batchLogModule($provide){ + $provide.factory('batchLog', ['$defer', '$log', function($defer, $log) { + var messageQueue = []; + + function log() { + if (messageQueue.length) { + $log('batchLog messages: ', messageQueue); + messageQueue = []; + } + $defer(log, 50000); + } - // start periodic checking - log(); + // start periodic checking + log(); - return function(message) { - messageQueue.push(message); + return function(message) { + messageQueue.push(message); + } + }]); + + /** + * routeTemplateMonitor monitors each $route change and logs the current + * template via the batchLog service. + */ + $provide.factory('routeTemplateMonitor', + ['$route', 'batchLog', '$rootScope', + function($route, batchLog, $rootScope) { + $rootScope.$on('$afterRouteChange', function() { + batchLog($route.current ? $route.current.template : null); + }); + }]); } -}, {$inject: ['$defer', '$log']}); -// note how we declared dependency on built-in $defer and $log services above -/** -* routeTemplateMonitor monitors each $route change and logs the current -* template via the batchLog service. -*/ -angular.module.ng('routeTemplateMonitor', function($route, batchLog) { - this.$on('$afterRouteChange', function() { - batchLog($route.current ? $route.current.template : null); - }); -}, {$inject: ['$route', 'batchLog'], $eager: true}); + // get the main service to kick of the application + angular.injector(batchLogModule).get('routeTemplateMonitor'); </pre> Things to notice in this example: @@ -57,11 +63,6 @@ Things to notice in this example: `console.log` in batches. * The `routeTemplateMonitor` service depends on the built-in {@link api/angular.module.ng.$route $route} service as well as our custom `batchLog` service. -* The `routeTemplateMonitor` service is declared to be eager, so that it is started as soon as the -application starts. -* To underline the need for the eager instantiation of the `routeTemplateMonitor` service, nothing -else in the application depends on this service, and in this particular case the factory function -of this service doesn't return anything at all. * Both of our services use the factory function signature as well as the `$inject` property to declare their dependencies. It is important that the order of the string identifiers in the array associated with the `$inject` property is the same as the order of argument names in the signature |
