Please read the code first
app.js
var app = angular.module('Nimbus', ['ngRoute']);
route.js
app.config(function($routeProvider) { $routeProvider .when('/login', { controller: 'LoginController', templateUrl: 'templates/pages/login.html', title: 'Login' }) .when('/home', { controller: 'HomeController', templateUrl: 'templates/pages/home.html', title: 'Dashboard' }) .when('/stats', { controller: 'StatsController', templateUrl: 'templates/pages/stats.html', title: 'Stats' }) }).run( function($q, $rootScope, $location, $route, Auth) { $rootScope.$on( "$routeChangeStart", function(event, next, current) { console.log("Started"); var canceler = $q.defer(); canceler.resolve(); }); $rootScope.$on("$routeChangeSuccess", function(currentRoute, previousRoute){ $rootScope.title = ($route.current.title) ? $route.current.title : 'Welcome'; }); })
homeowners controller.js
app.controller('HomeController', function HomeController($scope, API) { API.all(function(response){ console.log(response); }) } )
statistics-controller.js
app.controller('StatsController', function StatsController($scope, API) { API.all(function(response){ console.log(response); }) } )
api.js
app.factory('API', ['$q','$http', function($q, $http) { return { all: function(callback) { var canceler = $q.defer(); var apiurl = 'some_url' $http.get(apiurl,{timeout: canceler.promise}).success(callback); } } }]);
When I go from home to statistics, again the API will send an http request, I have many such HTTP requests, I have pasted only a few lines of code.
I need to have cancel cancel all pending http requests for routechangestart or success
Or any other way to implement the same?