Runoob Tutorial ArchiveEnglish

AngularJS Service

AngularJS Service

In AngularJS, you can create your own services or use built-in services.


What is a service?

In AngularJS, a service is a function or object that can be used in your AngularJS application.

AngularJS has built in over 30 services.

There is a$locationService, it can return the URL address of the current page.

Instance

var

app = angular.module(

'myApp'

, []);

app.controller(

'customersCtrl'

,

function

($scope, $location) {

    $scope.myUrl = $location.absUrl();

});

Try it »

Note$locationThe service is passed as a parameter to the controller. If you want to use it, you need to define it in the controller.


Why use the service?

In many services, such as the $location service, it can use objects that exist in the DOM, similar to the window.location object, but the window.location object has certain limitations in AngularJS applications.

AngularJS continuously monitors the application to handle event changes, AngularJS uses$locationThe service is better than usingthe window.locationobject.

$location vs window.location

  window.location $location.service
Purpose Allows read and write operations on the current browser location Allows read and write operations on the current browser location
API Expose a read/writeable object Expose a jQuery-style reader/writer
Whether it integrates with the AngularJS application lifecycle No Can obtain every stage in the application lifecycle and integrate with $watch
Whether it seamlessly integrates with HTML5 APIs No Yes (for graceful degradation to lower-level browsers)
Is it related to the context of the application No, window.location.path returns "/docroot/actual/path" Yes, $location.path() returns "/actual/path"

$http service

$httpIt is the most commonly used service in AngularJS applications. The service sends requests to the server, and the application responds to the data sent by the server.

Instance

Use$httpService requests data from the server:

var

app = angular.module(

'myApp'

, []);

app.controller(

'myCtrl'

,

function

($scope, $http) {

    $http.get(

"welcome.htm"

).then(

function

(response) {

$scope.myWelcome = response.data;

});

});

Try it »

The above is a very simple$httpservice instance, more$httpPlease check the service applicationAngularJS Http tutorial.


$timeout service

AngularJS$timeoutThe service corresponds to the JS window.setTimeout function.

instance

Display information after two seconds:

var

app = angular.module(

'myApp'

, []);

app.controller(

'myCtrl'

,

function

($scope, $timeout) {

    $scope.myHeader =

"Hello World!"

;

    $timeout(

function

() {

$scope.myHeader =

"How are you today?"

;

},

2000

);

});

Try it »


$interval service

AngularJS$intervalThe service corresponds to JSwindow.setIntervalfunction.

instance

Display information every second:

var

app = angular.module(

'myApp'

, []);

app.controller(

'myCtrl'

,

function

($scope, $interval) {

    $scope.theTime =

new

Date().toLocaleTimeString();

    $interval(

function

() {

        $scope.theTime =

new

Date().toLocaleTimeString();

},

1000

);

});

Try it »


Create custom service

You can create custom services and link them to your modules:

Create a service namedhexafy:

app.service(

'hexafy'

,

function

() {

    this.myFunc =

function

(x) {

       

return

x.toString(

16

);

}

});

To use custom services, you need to add them independently when defining the controller and set the dependencies:

Instance

Using a custom servicehexafyConvert a number to a hexadecimal number:

app.controller(

'myCtrl'

,

function

($scope,

hexafy

) {

    $scope.hex =

hexafy

.myFunc(

255

);

});

Try it »


Use custom services in filters

Once you create a custom service and connect it to your application, you can use it in controllers, directives, filters, or other services.

In the filtermyFormatusing the servicehexafy:

app.filter(

'myFormat'

,

'hexafy'

,

function

(

hexafy

) {

   

return

function

(x) {

       

return

hexafy

.myFunc(x);

    };

}]);

Try it »

You can use filter when getting values from an object array:

Create servicehexafy:

<
ul
<
li
ng-repeat=

"x in counts"

{{x | myFormat}}

<
/li
<
/ul

Try it »