Service in AngularJS is the functional block for providing data to components(controllers, services, filters or directives) across your app. You can write your own services. You can also use the built-in services in AngularJS, like $http. To use a service, you need to add it as a dependency for your component.

The steps for creating and using a service are as follows:

  • Creating a service
app.service('myService', function(){
  // Runs during compile
  return {
    name: xiaoming
  };
});
  • Using a service
app.controller('myCtrl', ['$scope', 'myService', function($scope, myservice) {
  $scope.name = myService.name;
}]);

You can also use the method factory to register a service as follows:

app.factory('myService', function(){
  // Runs during compile
  return {
    name: xiaoming
  };
});

As I said, you can also use many of the AngularJS built-in services. The ones you may use frequently are $http, $q, $animate, $log, $timeout, $window and $rootScope`, etc..