AngularJS Tutorial

Define AngularJS

Download AngularJS

AngularJS Expression

AngularJS vs Javascript

AngularJS Directives

AngularJS Module

AngularJS Controller

Custom Directive

AngularJS Scope

AngularJS Validation

AngularJS Filter

AngularJS SelectBox

AngularJS Directive

AngularJS Services

AngularJS Directives

AngularJS Routing

Interview Questions

AngularJS Services

Services are javascript functions and are responsible to do a specific tasks. This makes them an individual entity which is maintainable and testable. Controllers, filters can call them as on requirement basis. AngularJS provides many inbuilt services like, $https:, $route, $window, $location etc. Inbuilt services are always prefixed with $ symbol.


$location service

The $location service has methods which return information about the location of the current web page.

Example: Display the page URL.

<h3>{{myUrl}}</h3>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $location) {
    $scope.myUrl = $location.absUrl();
});
</script>

The $http Service

The $http service is one of the most common used services in AngularJS applications. The service makes a request to the server, and lets your application handle the response.

<h1>{{text}}</h1>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $http.get("file.html").then(function (response) {
      $scope.text = response.data;
  });
});
</script>

The $timeout Service

Example: Display a new message after two seconds.

<h1>{{myHeader}}</h1>

<script>var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
    $scope.myMessage = "Hello World!";
    $timeout(function () {
        $scope.myMessage = "How are you today?";
    }, 2000);
})</script>;

The $interval Service

Display the time every second

<h1>{{theTime}}</h1>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
  $scope.theTime = new Date().toLocaleTimeString();
  $interval(function () {
      $scope.theTime = new Date().toLocaleTimeString();
  }, 1000);
});
</script>