Introduction to angular.js services

Services is a unit of code which can be used across the project controllers. Means when we create a service we can use it in any controllers of our project. Services required when we need to use the same code in multiple places.

Services can be utilities or business logic unit which means whenever you write any kind of code as part of a service you had to consider that code to be almost independent of that particular controller. It also some business logic code.

Why services –

  • 1-Services provides lots of built-in functionality
  • 2-Best place to implement reusable code
  • 3-Clean and maintainable code

Features of services

  • 1-Service is a singleton means you have only one instance of particular services. The Same instance is maintained throughout the lifetime of application
  • 2-Once services instance created they are living all time in application unlike controller are destroyed when the routing change because controllers are dynamic in nature.
  • 3- Services are loaded and instantiated lazily means services are loaded when the controller is instantiated. When a controller refers the respective service which they want to use only that time service load.
  • 4- You can use multiple services in a single controller or multiple controllers
  • 5- Services can use another service
  • 6- You have one business logic in a service and you can to reuse same logic in another service.

Types of services

Built-in services

built-in services are part of the angular framework which means angular by default come by own services.

Ex.

$http,$log,$location, etc
$http is used for AJAX based communication.
Custom services

custom services means you can develop your own services

You can create custom services with 5 types

  • 1- Provider
  • 2- Service
  • 3- Factory
  • 4- Value
  • 5- Constant

I hope this article will help you to use services in angular. Keep checking Technoalerts.com for more cool article like this

check the following link for more details about angular services with example

https://www.technoalerts.com/angular-js/use-custom-services-providerservicefactoryvalueconstant/

Leave a Reply

Your email address will not be published. Required fields are marked *