LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

AngularJS Services


In AngularJS you can make your very own administration, or utilize one of the many inherent services.


What is a Service?

In AngularJS, an administration is a capacity, or article, that is accessible for, what's more, restricted to, your AngularJS application.

AngularJS has around 30 worked in administrations. One of them is the $location service.

The $location administration has techniques which return data about the area of the present web page:

Example

Use the $location administration in a controller:

var application = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $location) {
    $scope.myUrl = $location.absUrl();
});
Attempt it Yourself »

Note that the $location administration is passed in to the controller as a contention. So as to utilize the administration in the controller, it must be characterized as a dependency.


Why use Services?

For numerous administrations, similar to the $location administration, it appears you could utilize objects that are as of now in the DOM, similar to the window.location article, and you could, however it would have a few confinements, at any rate for your AngularJS application.

AngularJS always regulate your application, and for it to deal with changes furthermore, occasions legitimately, AngularJS inclines toward that you utilize the $location administration rather than the window.location object.


The $http Service

The $http administration is a standout amongst the most widely recognized utilized administrations in AngularJS applications. The administration makes a demand to the server, and lets your application handle the response.

Example

Use the $http administration to ask for information from the server:

var application = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
    $http.get("welcome.htm").then(function (reaction) {
        $scope.myWelcome = response.data;
    });
});
Attempt it Yourself »

This model shows an exceptionally basic utilization of the $http administration. Find out additional about the $http administration in the AngularJS Http Tutorial.


The $timeout Service

The $timeout administration is AngularJS' rendition of the window.setTimeout function.

Example

Display another message after two seconds:

var application = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
    $scope.myHeader = "Hi World!";
    $timeout(function () {
        $scope.myHeader = "How are you today?";
    }, 2000);
});
Attempt it Yourself »

The $interval Service

The $interval administration is AngularJS' variant of the window.setInterval function.

Example

Display the time each second:

var application = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
    $scope.theTime = new Date().toLocaleTimeString();
    $interval(function () {
        $scope.theTime = new Date().toLocaleTimeString();
    }, 1000);
});
Yourself »

Create Your Own Service

To make your own administration, associate your support of the module:

Create an administration named hexafy:

app.service('hexafy', work() {
    this.myFunc = work (x) {
        return x.toString(16);
    }
});

To utilize your hand crafted administration, include it as a reliance when characterizing the channel:

Example

Use the uniquely designed administration named hexafy to change over a number into a hexadecimal number:

app.controller('myCtrl', function($scope, hexafy) {
    $scope.hex = hexafy.myFunc(255);
});
Attempt it Yourself »

Use a Custom Service Inside a Filter

Once you have made an administration, and associated it to your application, you can utilize the administration in any controller, order, channel, or even inside other services.

To utilize the administration inside a channel, include it as a reliance when characterizing the filter:

The administration hexafy utilized in the channel myFormat:

app.filter('myFormat',['hexafy', function(hexafy) {
    return function(x) {
        return hexafy.myFunc(x);
    };
}]);
Attempt it Yourself »

You can utilize the channel while showing esteems from an item, or a cluster:

Create an administration named hexafy:

<ul>
<li ng-repeat="x in counts">{{x | myFormat}}</li>
</ul>
Attempt it Yourself »