Angular Zen
Any service managed by AngularJS' DI mechanism needs to have a unique name
(for example, notificationsArchive in the preceding example). What follows is
a recipe for creating new instances.
Value objects are not particularly interesting, since object registered via this
method can't depend on other objects. This is not much of the problem for the
NotificationArchive instance, since it doesn't have any dependencies. In practice,
this method of registration only works for very simple objects (usually expressed as
instances of built-in objects or object literals).
Services
We can't register the NotificationsService service as a value object, since we need
to express a dependency on an archive service. The simplest way of registering a
recipe for objects, depending on other objects, is to register a constructor function.
We can do this using the service method as follows:
myMod.service('notificationsService', NotificationsService);
where the NotificationsService constructor function can now be written
as follows:
var NotificationsService = function (notificationsArchive) {
this.notificationsArchive = notificationsArchive;
};
By using AngularJS dependency injection we could eliminate the new keyword from
the NoficiationsService constructor function. Now this service is not concerned
with dependencies instantiation and can accept any archiving service. Our simple
application is much more flexible now!
A service is one of those overloaded words that might mean many
different things. In AngularJS the word service can refer to either the
method of registering constructor functions (as shown in the previous
example) or any singleton object that is created and managed by
AngularJS DI system, regardless of the method of registering used
(this is what most people mean by using the word service in the
context of AngularJS modules).
In practice the service method is not commonly used but might come in handy for
registering pre-existing constructor functions, and thus make AngularJS manage
objects created by those constructors.