0 votes
in Angular by
What are Services in Angular?

1 Answer

0 votes
by

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.

JavaScript
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.

HTML
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.

Related questions

0 votes
asked Dec 31, 2023 in Angular by DavidAnderson
0 votes
asked Dec 31, 2023 in Angular by DavidAnderson
...