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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -