Beginning AngularJS

(WallPaper) #1

Chapter 7 ■ ServiCeS and Server CommuniCation


return dateTimeSvc;


}).controller("MyController", function ($scope, dateTimeService) {


$scope.theDate = dateTimeService.getDate();
$scope.theTime = dateTimeService.getTime();


});





{{theDate}}


{{theTime}}




In Listing 7-4, you can see that we use the factory method on our module. This method takes two arguments, the
first of which is the name of our service. We named this service dateTimeService. The second argument is the factory
function, which returns an object. This object is known as the service object, and it represents the service that you will
ultimately use in your application.
When the application first needs to use this service, the framework will call the factory function. In this example,
the service object that it creates and returns is called dateTimeSvc, and it is this object that is used whenever the
service is needed again. In other words, this service object, once created, is common to the entire application. This is
a very important point, because it means that changes made to the state of this object remain in play throughout the
lifetime of the application. We will see the implications of this later in the chapter.
As the primary purpose of our factory function is to create an object with our service’s functionality, we busy
ourselves doing just that. We set up an empty dateTimeSvc object, and then we attach to it the two service methods:
getDate() and getTime(). We finish by specifying dateTimeSvc as the return value.
With the service in place, it’s time to turn our attention to our controller, so that we can find out how to make use
of it. The main thing to note about the controller function is that its second argument, the anonymous function, asks
for the dateTimeService in exactly the same way that we have already seen when looking at the built-in services. As
we registered our service using the name dateTimeService, Angular has no problem resolving this dependency for us.
Figure 7-1 shows the output of Listing 7-4. The result is simply two paragraphs containing the return values of the
calls we made on our service.


Figure 7-1. The dateTimeService in action

Free download pdf