javascript - Create a custom http service that will request external JSON file. pure angularjs only -
i have http service call external json files , load grid.
my problem need create custom http service use same in different controllers. function of custom service should same (requesting external json file)
$http.get('../json/permanentemployees.json'). success(function(data, status, headers, config) { $scope.masterarray = data; }). error(function(data, status, headers, config) { $scope.errormessage = "requested grid data file not exist"; });
this current http service. appreciated.please use angularjs only
wrap code in factory , use it. think better use factory in situation, refer here. p.s. unable create mockup of request json, please check json.
jsfiddle: here
app.factory('customhttpservice', function($http){ return { getrequest: function(url) { return $http.get(url). success(function(data, status, headers, config) { return {data: data, errormessage: "success"}; }). error(function(data, status, headers, config) { return {data: "", errormessage: "requested grid data file not exist"}; }); } } });
in controller can
app.controller('mycontroller', function mycontroller($scope, customhttpservice) { $scope.data = customhttpservice.getrequest('../json/permanentemployees.json').data; });
Comments
Post a Comment