Creating services

In AngularJS, a service is a singleton object that has its life cycle controlled by the framework. It can be used by any other component such as controllers, directives, filters, and even other services.

Now, it's time to evolve our application, introducing new features in order to calculate the parking time and also the price.

To keep high levels of cohesion inside each component, we must take care of what kind of behavior is implemented in the controller. This kind of feature could be the responsibility of a service that can be shared across the entire application and also tested separately.

In the following code, the controller is delegating a specific behavior to the service, creating a place to evolve the business rules in ...

Get AngularJS Essentials now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.