javascript - How to avoid repeating http requests angularJS -


is there way in angular avoid repeating http requests? can see in code above i'm making call retrieve detailed info of product. fact call associated button... avoid repetitive calls. if have clicked on detailed-product-button don't need make call again service....the proper way load info once , show , hided; don't know how manage on angular. (also don't want load detail product scrach product, want loaded on user's clic demand, once)

$scope.seeinfo= function(id){      $http.get('/shop/getdetailinfo/'+id).         success(function(data, status) {             $scope.info = data.detailinfo;             if (data.detailinfo>0) $scope.showdetails=true;             else $scope.showdetails=false;         });  }; 

you can store every item user request in factory , check if content in factory before ajax call.

   $scope.seeinfo= function(id){         var detailinfo = somefactory.get(id); //get item data factory if exist         if (angular.isundefined(detailinfo) || detailinfo === null) {             $http.get('/shop/getdetailinfo/'+id).                 success(function(data, status) {                     somefactory.set(id, data); //set ajax data factory                     $scope.info = data.detailinfo;                     if (data.detailinfo>0) $scope.showdetails=true;                     else $scope.showdetails=false;                 });             }         } else {             $scope.info = detailinfo;             if (detailinfo>0) $scope.showdetails=true;             else $scope.showdetails=false;         }     }; 

as said can use $http cache don't know how works

update

a somefactory example:

.factory('somefactory', [function() {      var storeditems = [];      return {         get: function(id) {             return storeditems[id];         },         set: function(id, data) {             storeditems[id] = data;         }     };  }]); 

test factory:

somefactory.set(12345, {"info":"hello"}); somefactory.set(2, "world");  console.log(somefactory.get(12345).info); // returns hello console.log(somefactory.get(2)); //returns world 

you can store strings, objects....

hope helps you

update2 full example code

var someapp = angular.module("someapp", [])  .controller('somectrl', ['somefactory', function(somefactory) {    somefactory.set(12345, {"info":"hello"});   somefactory.set(2, "world");    console.log(somefactory.get(12345).info); // returns hello   console.log(somefactory.get(2)); //returns world  }]).factory('somefactory', [function() {      var storeditems = [];      return {         get: function(id) {             return storeditems[id];         },         set: function(id, data) {             storeditems[id] = data;         }     };  }]); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -