5
$scope.$on('$destroy', function (event){    
        $timeout.cancel(promiseObj);    
    });

If i am on a page that is being loaded(since the page contain $http request, it takes time to load data) and while loading, I change the page from navigation, the $timeout is not being deleted, and continuous http call are going. can you help?

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
Omkar Telee
  • 77
  • 1
  • 7

2 Answers2

1

Use $routeChangeStart instead of $destroy

$routeChangeStart

Broadcasted before a route change. At this point the route services starts resolving all of the dependencies needed for the route change to occur. Typically this involves fetching the view template as well as any dependencies defined in resolve route property. Once all of the dependencies are resolved $routeChangeSuccess is fired.

The route change (and the $location change that triggered it) can be prevented by calling preventDefault method of the event. See $rootScope.Scope for more details about event object.


So please try this below code.

$scope.$on('$routeChangeStart', function (scope, next, current) {
            if (next.$$route.controller != "Your Controller Name") {
               $timeout.cancel(promiseObj);// clear interval here
            }
        });
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • i tried using above code with changing the controller name, the $scope.$on method is not being called..so remaining code is not being executed with event change. In short the event of rootChangeStart is not happening for me. – Omkar Telee Apr 22 '15 at 05:31
1

Actually, problem was angular created many objects with same name as promiseObj. So, those object were not being deleted. So, I created an array of promiseObj[], and using for loop i deleted all the promises. ;)

$scope.$on('$destroy', function () {    
    for(var promise in promiseObj)
    {
        $timeout.cancel(promiseObj[promise]);
    }
});
Omkar Telee
  • 77
  • 1
  • 7