Follow by Email

AngularJS Built-in services


AngularJS provides with many in built services, but some of the important services for developing a web application are covered in this tutorials.


Service is a class or an object, which works on singleton pattern once you create an object of a class the same object will be reused so that the memory utilization is very less.

Inbuilt services in AngularJS are the existing object created out of a class that is reused in the entire application. The class is created in the AngularJs library itself and inbuilt services are the objects of those classes.
Some of the important inbuilt services in AngularJS are as follows:

$location:
The $location service has methods which return information about the location of the current web page, list of methods supported by $location are :

$location.absUrl();       -   Provides the url of the application

$location.url([url]);       -    Provides the path after the “#” tab.

$location.protocol();    -    Provides the protocol of the application url.

$location.host();      -    Provides the host name of your application.

$location.port();      -   Provides the port name of your application.

$location.path([path]);    -  Provides your current path after the “#” tag.

$location.search(search, [paramValue]);   -  Provides search part (as object) of current URL.

$location.hash([hash]);     -  Provides the hash value at the end of the url

$location.replace();    -  Perform changes to $location during the current $digest will replace the current history record.

$location.state([state])   -  Provides State object for pushState or replaceState.


There Javascript objects like window.location which handles your location object but it does provide will all the features like AngularJS $location.
When you AngularJS it is preferred to use the $location service instead of the window.location object.


$http:
The $http service is used to make http request to third party application or server side application or API calls. The service makes a request to the server, and lets your application handle the response.

$http service will internally work like xmlhttp request in Javascript but the only advantage here is AngularJs provides shortcut methods like:

$http.get('/endpoint(url)', config).then(successCallback, errorCallback);
$http.post('/endpoint(url)', data, config).then(successCallback, errorCallback);


Below are the list of shortcut http methods:

    $http.get      -   Used to make a GET API call

    $http.head   -   Used for object created by a call to $sce.trustAsResourceUrl(url).

    $http.post    -   Used to make a POST API call

    $http.put      -   Used to make a PUT API call

    $http.delete   - Used to make a DELETE API call

    $http.jsonp    - Used to send jsonp datatype in the API call

    $http.patch    - Used to perform PATCH request