Categories: AngularJs

Services – AngularJS

Services are JavaScript functions that are designed to perform some specific operation. These services can be called by controllers and filters etc.
There are many built in service available in AngularJS. You can also design Your own custom services.

Some of built-in service are:
$location
$http
$timeout – Works as window.setTimeout in javascript.
$interval – works as window.setInterval in javascript
etc

Using $location service:

[html]

{{url}}

app.controller('serviceCtrl', function($scope,$location) { $scope.url = $location.absUrl() });

[/html]

Using $http Service:

$http service is used most often in AngularJS application. $http service used to make request to the server and getting response.

[html]

{{response}}

Myapp.controller('serviceCtrl', function($scope,$http) { $http.get("tryit/angularjs/hello.html").then(function (responseData) { $scope.response = responseData.data; }); });
[/html]

Custom Services:
You can create your own custom service. For that you need to create a service and connect it to module. While creating controller you need to add it as a dependency. After service is created and connected to application, it can be used in any any where like controller, filters or other services.

[html]

Num:


Result Square: {{resultDesc}}

myApp.controller('myController', function($scope, customeService) { $scope.multiply = function() { $scope.resultDesc = customeService.multiply($scope.num); } });

[/html]

jeff77

Disqus Comments Loading...

Recent Posts

Noun

A Noun is the name of a person, place or thing. or Name of anything is noun. For example: Sita…

10 years ago

Introduction – AngularJS

Misko Hevery, a Google employee, developed AngularJS. First version 1.0 of AngularJS was released in 2012. Now AngularJS is fully…

9 years ago

Expressions – AngularJS

AngularJS expressions contain operators,literals, and variables.  AngularJS expressions can be written inside HTML while JavaScript expressions can't be.  AngularJS expressions…

9 years ago

Noun – Exercise

Some sentences are given below. Choose the nouns and mention the type of noun whether they are proper noun, common…

9 years ago

Modules – AngularJS

Modules are containers for appication controller. They contain different parts of an application. [html] {{ state + " " +…

9 years ago

Directives – AngularJS

Using Directives you can extend HTML attributes. In AngularJS you can define your own directives while it has its own…

9 years ago