Latest web development tutorials

AngularJS Service (Service)

AngularJS you can create your own service, or use the built-in service.


What is a service?

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

AngularJS built more than 30 services.

There is a$ location service, which returns the URL address of the current page.

Examples

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

try it"

Note that$ location service is passed as a parameter to the controller.If you want to use it, you need to be defined in the controller.


Why use the service?

$ http AngularJS application is the most commonly used services.The service sends a request to the server, the application server response data transmitted from.

AngularJS constantly monitors application, handle events change, AngularJS use$ location serve better than using window.location.


$ Http service

$ http AngularJS application is the most commonly used services.The service sends a request to the server, the application server response data transmitted from.

Examples

Use$ http service request data to 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$ http service instance, more $ httpservice applications Please see AngularJS Http tutorial .


$ Timeout Service

AngularJS $ timeout and services corresponding to the JSwindow.setTimeout function.

Examples

Information displayed 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 $ interval corresponding to the JSwindow.setInterval service function.

Examples

Every two seconds to display information:

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"

Creating a Custom Service

You can create a custom service access, links to your module:

Create access namedhexafy of:

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

To access the custom service, you need to define the filter when the Add Standalone:

Examples

Use a custom servicehexafy a digital converter hexadecimal numbers:

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

try it"

Filter using custom service

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

Hexafy use the service in the filter myFormatin:

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

try it"

Getting value in an array of objects you can use filters:

Create Servicehexafy:

<Ul>
<Li ng-repeat = "x in counts"> {{x | myFormat}} </ li>
</ Ul>

try it"