Service helps to implement dependency injection. For instance, let’s say we have the below Customer
class who needs Logger
object. Now Logger
object can be of FileLogger
type or EventLogger
type.
function Customer($scope,$http, Logger)
{
$scope.Logger = Logger;
}
So you can use the service
method of the application and tie up the EventLogger
object with the Logger
input parameter of the Customer
class.
var app = angular.module("myApp", []); // creating a APP
app.controller("Customer", Customer); // Registering the VM
app.service("Logger", EventLogger); // Injects a global Event logger object
So when the controller
object is created, the EventLogger
object is injected automatically in the controller
class.