How to create and use services and factories in AngularJS
Mar 13, 2016 • Kevin Hou
What Are Services?
In a nutshell, services are a way of making functions and variables global. They can be injected as dependencies in any controller and its functions/variables can be accessible across controllers.
The way I explained this to a friend is basically using the 'ui.bootstrap' analogy. Bootstrap in its most basic form is a a compilation of CSS styling and Javascript for functionality. When we include the 'ui.bootstrap' dependency, we are essentially including that Javascript file to be used in this controller. It's not different when we create our own services. A service is basically a group of Javascript functions. If the services is included as a dependency, it will load all those Javascript functions and make them available to use in that controller. It's a good way of selectively loading specific Javascript files so that not all of your Javascript files have to load for every page.
Include the name of the service function in the controller’s function(…, …)