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. The responsibility of a service 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 the future.

parking.controller("parkingCtrl", function($scope, parkingService) {
$scope.appTitle = "Parking";
$ = [];
$scope.colors = ["White", "Black", "Blue", "Red", "Silver"];
$scope.park = function(car) {
car.entrance = new Date();
delete $;
$scope.calculateTicket = function(car) {
$scope.ticket = parkingService.calculateTicket(car);
Related Tutorial
7. replace
9. scope
11. link
12. require
14. compile