diff options
Diffstat (limited to 'docs')
3 files changed, 37 insertions, 43 deletions
diff --git a/docs/content/guide/dev_guide.services.creating_services.ngdoc b/docs/content/guide/dev_guide.services.creating_services.ngdoc index 397f86c7..06d57d77 100644 --- a/docs/content/guide/dev_guide.services.creating_services.ngdoc +++ b/docs/content/guide/dev_guide.services.creating_services.ngdoc @@ -14,9 +14,6 @@ The `angular.module.ng` method accepts three parameters: - `$inject` - {Array.<string>} - Array of service ids this service depends on. These services will be passed as arguments into the factory function in the same order specified in the `$inject` array. Defaults to `[]`. - - `$eager` - {boolean} - If true, the service factory will be called and the service will be -instantiated when angular boots. If false, the service will be lazily instantiated when it is first -requested during instantiation of a dependant. Defaults to `false`. The `this` of the factory function is bound to the root scope of the angular application. 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 diff --git a/docs/content/guide/dev_guide.services.registering_services.ngdoc b/docs/content/guide/dev_guide.services.registering_services.ngdoc index 24a21852..25a51504 100644 --- a/docs/content/guide/dev_guide.services.registering_services.ngdoc +++ b/docs/content/guide/dev_guide.services.registering_services.ngdoc @@ -33,13 +33,9 @@ angular.module.ng('service id', function() { var shinyNewServiceInstance; //factory function body that constructs shinyNewServiceInstance return shinyNewServiceInstance; -}, {$eager: true}); +}); </pre> -While it is tempting to declare services as eager, only in few cases it is actually useful. If you -are unsure whether to make a service eager, it likely doesn't need to be. To be more specific, a -service should be declared as eager only if it fits one of these scenarios: - * Nothing in your application declares this service as its dependency, and this service affects the state or configuration of the application (e.g. a service that configures `$route` or `$resource` services) |
