In Angularjs's actual project, it is often necessary to process multiple $http requests, each $HTTP request returns a promise, and we can put multiple promise into an array argument accepted by the $q.all () method.
Processing multiple $HTTP Requests
Angular.module (' app ', []). Controller (' Appctrl ',functionAppctrl (myservice) {varApp = This; Myservice.getall (). Then (function(info) {App.myinfo=info; }). Service (' MyService ',functionMyService ($http, $q) {varMyService = This; User= ' Https://api ... ', Repos= ' ', Events= ' '; Myservice.getdata=functionGetData () {return$http. Get (user). Then (function(userData) {return{name:userData.data.name, Url:userData.data.url, Repocount: UserData.data.count})}; Myservice.getuserrepos=functionGetuserrepos () {return$http. Get (Repos). Then (function(response) {return_.map (Response.data,function(item) {return{name:item.name, description:item.description, Starts:item.startCount}})} Myservice.getusereve NTS=functiongetuserevents () {...} Myservice.getall=function(){ varUserpromise =myservice.getdata (), Usereventspromise=Myservice.getuserrepos (), Userrepospromise=Myservice.getuserrepos (); return$q. All ([Userpromise, Usereventspromise, Userrepospromise]). Then (function(){ .... }) } })
$http Request Caching
The $http get method The second parameter accepts an object that has a cache field that can accept a bool type to implement caching, or {cache:true}, or a service can be accepted.
Create a service by factory and inject the service into the controller.
Angular.module (' app ', []). Factory ("Mycache",function($cacheFactory) {return$cacheFactory ("Me"); }). controller ("Appctrl",function($http, Mycache) {varApp = This; App.load=function() {$http. Get ("Apiurl", {Cache:mycache}). Success (function(data) {App.data=data; })} App.clearcache=function() {Mycache.remove ("Apiurl"); } })
Above
In fact, implementing the caching mechanism is $cachefactory
Placing the caching mechanism in the current request via {Cache:mycache}
$cacheFactory the request API as a key, so when the cache is clear, it is also based on this key to clear the cache
$http cache in Angularjs and processing of multiple $HTTP requests