Using AngularJs Service to fetch data

Angular js has lot of features but the one I like most is its services, well it has given the user to build a custom service and inject that service in to angular js components like controller where the data will be fetched from that service.

Angular Services are singleton for example if you store some retrieved data in the service as a variable and access that variable from various services or from various controller still it will give you the same data as it wont create multiple instances of the service.

In our current application we have just hard coded the data in our mainController as $ and in this tutorial we are going to bring the cars array form a carService which we are going to build in few minutes. Lets create a new js file in our services directive under js folder which we have created in the previous tutorials.


app.service('carService',function(){     function getCarDetails(){         return "";     }    return{        getCarDetails:getCarDetails    }  });

In the above code we used app which is our angular app defined in the mainController and called a method from app which accepts two parameters. First is the string of service name “carService” and second is a function which is going to return/expose a method called getCarDetails. This method is then used from different places to get the car details. Now lets return the car array from this service.


app.service('carService',function(){ //Hard coded cars Array     var cars = [                {url:"",                 name:"SHELBY",                 desc:"The Shelby Mustang is a high performance variant of the Ford Mustang which was built by Shelby from 1965 to 1968, and from 1969 to 1970 by Ford. Following the introduction of the fifth generation Ford Mustang in 2005, the Shelby nameplate was revived as a new high-performance model, this time designed and built by Ford.[1]"},                {url:"",                 name:"BMW",                 desc:"BMW was established as a business entity following a restructuring of the Rapp Motorenwerke aircraft manufacturing firm in 1917. After the end of World War I in 1918, BMW was forced to cease aircraft-engine production by the terms of the Versailles Armistice Treaty.[5] The company consequently shifted to motorcycle production as the restrictions of the treaty started to be lifted in 1923,[6] followed by automobiles in 1928â29"},                {url:"",                 name:"LANDROVER",                desc:"Land Rover is a car brand that specialises in four-wheel-drive vehicles, owned by British multinational car manufacturer Jaguar Land Rover, which is in turn owned by India's Tata Motors since 2008.[4] The Land Rover is regarded as a British icon, and was granted a Royal Warrant by King George VI in 1951"}               ];          function getCarDetails(){         return cars;     }    return{        getCarDetails:getCarDetails    }  });

Now lets see how we can use this new service in our mainController. Before injecting this service in controller we need to do few things.

  • Add this carService.js in the index.html as script reference.
  • Remove the cars array from controller but keep the $ as we will be fetching the data from the service and assigning it to $

Now lets do some angular injection…


app.controller('mainController',function($scope,carService){     $ = carService.getCarDetails();     $scope.desc = $[0].desc;     $scope.onSelect = function(index){        $scope.desc= $[index].desc;     } });

The only change we did is moved the cars hardcoding from mainController to carService and injected carService in the mainController and assigned carService.getCarDetails() to $ rest everything is same. The app will be working as expected with the data part moved to service layer. For now we have hardcoded the cars array in carService itself. In the next tutorial we will get the cars array form a JSON service and we will be using $http service to fetch data from the service. Happy Coding.

Download Demo


Leave a Reply

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