What is an eager property which is used to initialize the eager services. injector.eager()

283 views
Skip to first unread message

Hari

unread,
Dec 23, 2011, 12:35:49 AM12/23/11
to AngularJS
I was going through the documentation and have the above question.
In anguarjs documentation of injector, it talks about eager service. I
am not quite sure what this means. If some one can explain this, I
would appreciate it ...

Dan Doyon

unread,
Dec 23, 2011, 2:15:53 AM12/23/11
to ang...@googlegroups.com
Taking from the ng doc, below is a non-eager service, basically, angular will not create this service until it is called the first time. So could you have a bunch of services defined but  only the ones you are actually using are created, you don't incur the cost of them until you use them. On the other hand adding a $eager: true attribute in the properties object so {$inject: ['$window'], $eager: true},  tells angular that this is a service you want loaded right away because your controllers or other services will depend on it being available.  Examples of where you would want to be eager might be a routing service or logging service. 

angular.module.ng('notify', function(win) {
  var msgs = [];
  return function(msg) {
    msgs.push(msg);
    if (msgs.length == 3) {
      win.alert(msgs.join("\n"));
      msgs = [];
    }
  };
}, {$inject: ['$window']});

--dan


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.


Vojta Jina

unread,
Dec 27, 2011, 5:49:47 PM12/27/11
to ang...@googlegroups.com
Yep, Dan is right:
- eager services are instantiated at the very beginning (when app is being bootstrap)
- non-eager services are instantiated only when needed, so if your code does not ask for non-eager service, it will never be instantiated

Btw, don't worry about it, we are getting rid off "eager" :-D

V.

Luke Bayes

unread,
Feb 10, 2012, 5:41:53 PM2/10/12
to ang...@googlegroups.com
Hey Guys,

Just following up on this thread.

Did you wind up getting rid of $eager?

We're about to need it for a KeyService.

We'd like to track DOM key events on the $document element, but then emit them something like the following pseudocode:

$window.document.addEventListener('keyup', function(domEvent) {
  var activeElement = $window.document.activeElement;
  var currentScope = activeElement ? angular.element(activeElement).scope() : $route.current.scope;
  currentScope.$emit(domEvent.name, domEvent);
});

This will give our controllers and other nested scopes the ability to handle key events and consider cancelling them if necessary.

But we'd rather not just inject this service into some arbitrary feature in order to instantiate it.

Please let us know.

Thanks,

Luke Bayes


Vojta Jina

unread,
Feb 10, 2012, 5:53:13 PM2/10/12
to ang...@googlegroups.com
Hey Luke, yep, eagers are gone.

But modules have a run method, where you can do this kind of stuff.

var app = angular.module('myApp', []);

app.run(function($window) {
  angular.element($window).bind('keyup', function() {
    // ....
  });
});


V.
Reply all
Reply to author
Forward
0 new messages