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.

controllers.js
snippet
parking.controller("parkingCtrl", function($scope, parkingService) {
$scope.appTitle = "Parking";
$scope.cars = [];
$scope.colors = ["White", "Black", "Blue", "Red", "Silver"];
$scope.park = function(car) {
car.entrance = new Date();
$scope.cars.push(car);
delete $scope.car;
};
$scope.calculateTicket = function(car) {
$scope.ticket = parkingService.calculateTicket(car);
};
});
Related Tutorial
7. replace
9. scope
11. link
12. require
14. compile
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 # # # # #