Friday, March 21, 2014

To make the ajax calls we use the $http in angularjs

$http is a obejct it has several methods like get, post, delete

The below is the example of creating the service:

.service('clusterresult', function($http){
this.getResult = function (){
            return $http.get('DataFiles/clusterDetails.json');
        };
this.getItems = function () {
            return $http.get('DataFiles/items.json');
        }
})

In the above example clusterresult is the name of the service, in other we also called it as a class.

so inside this service we creted the method or function to return or get teh data.

so to access the data we need to call the service name.method or function name, so as per the above example clusterresult.getResult()

service name is a class inside that we create the methods.

creating the controller with dependency injection:

.controller('homeController',['$scope','clusterresult',function($scope,clusterresult,$http){ ............  }]);

in the above example homeController is the name of the controller and

inside the [] square brackets '$scope','clusterresult' are dependencies that means these are available to controller,we can make use of them as parameters inside the function

i.e function($scope,clusterresult,$http), here the names we mentioned at both places are same. we can also give the names as we like inside the function

i.e function(a,b) here a refer the '$scope'  and b refer the 'clusterresult'.

so these are available for the function we used as a parameters

No comments:

Post a Comment