Connecting angularjs to firebase

As seen some of the previous tutorials, AngularJS provides some in-build services like $http, $location, etc. There may be chances while creating an application that the in-build services are not sufficient and we may need to create our own service.

Services are nothing but a block of code which is used to perform some repetitive task. So rather can duplicating the code, we use services. So use a service in your controller you have to inject it into it using Dependency Injection.

Let see an example that how we use a already provided service in our angular code.


// the $http service can be used here


To actually use this service in our angular code, like in a controller, we need to inject it in the dependencies section of the controller.

angular.module(‘DataApp’). Controller(‘testController’,function(dataService){



Note that the services in AngularJS are singleton i.e. a service will be initialised once and everywhere else that same instance of the service will be used.

Services are initialised by Angular only when a component is found which has a dependency on that service. This is called lazily initialised. But in case a service is heavy and may take some time to get load and initialised, we will have to manually initialise it when the module loads rather than waiting for the angular to do so after encountering some dependency. To do so, the run() method is used of module object. As soon as run() method is encountered, the service gets initialised.



// function code will come here




// service methods and properties can be used here

// the service has been initialized


Related Posts