AngularJS services

Having already played with several wonderful examples, we never gave a thought over how modularized our code has been written. We mostly did what looked simple and straightforward to get through the concepts and we are more tempted to write down every petty logic inside the controllers. But let’s now get to some coding standards and how we can increase the modularity of our AngularJS application.

It’s important that we keep our controller thin and readable, which means that every piece of business logic that needs to be shared across the application or across controllers should be taken care by some service layer. Service adds to better organize our application thereby boosting up the modularity level as well.

We can create our AngularJS services and Angular provides us with three ways to create and register our own service.

  1. Factory
  2. Service
  3. Provider

Controllers are primarily used to set-up initial state of the $scope object and add behavior (functions) to the $scope object. 

Let’s now try to create a user registration application as an example to what we have learnt until now.

All Services are singletons. They get instantiated once per app.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.