Beginning AngularJS

(WallPaper) #1
Chapter 7

Services and Server Communication


In the last chapter, we looked at HTML forms as a means of presenting a user interface for gathering a set of user
registration data. However, we didn’t look at the next step in the process, that is, sending that data along to a back-end
server for processing. In this chapter, we will achieve this by using Angular services. As Angular services are about
much more than sending data to servers, we will first look at them at a more general level.
The term service is rather overused in the development world, so what do we mean when we talk about Angular
services? A good way to think about an Angular service is as a set of tightly related functions, managed by the Angular
framework, which are made readily available for use across an application. For example, you might use something as
common as a company-wide data service, which enables any part of your application to send and retrieve data to and
from a corporate database. A marketing-and-communications-asset library, for example, which lets you locate and
retrieve images and image metadata, is much more specific. When speaking of Angular services, however, examples
such as these could fool anyone into thinking that services are all about server communication and data access, but
they are not. In Angular, getting a reference to the browser’s window object can also be achieved by using a service:
the built-in $window service. You can even create animations by using the built-in $animate service.
If we wanted to, we could create our own JavaScript object and give it a set of methods that performs a range of
related tasks. We could call upon this object whenever we needed it and, perhaps naively, describe it as a service. This
seems to fit closely the description I just gave you of an Angular service, but not quite. So, what is it about an Angular
service that makes it so special? While Angular ships with a set of very useful services, some of which we will look at
shortly, the answer to this question lies in the fact that it provides us with a framework within which services can be
easily managed. I say easily managed, because without this framework support, it wouldn’t be a trivial task.
If you were to tackle a task like this on your own, and you were serious about it, you would (at a minimum) have
to ask and answer the following questions:


•    When and where is the right place to instantiate my service?

•    What is the best way to manage service dependencies across my application?

•    What is the best approach for making sure that my services can be unit-tested and
configurable?

•    How should I handle persisting and sharing services between my controllers?

Of course, the answer to questions such as these, and many more just like them, have already been addressed by
the Angular team. As a developer, you need only learn some implementation details, but you can otherwise relax in
the knowledge that you are using a solid and well-thought-out solution. That, to me anyway, makes Angular services
pretty special.
Now that we know a little bit about what Angular services are at a high level, let’s dive in and have a look at a few
services that ship with Angular.

Free download pdf