How long is the promise of angular?

I am trying to understand the life cycle of angular services and components.

Let's say I have a controller that uses the http service:

function MyController($scope, $http) {
  $http.get('/service').success(function(data) {
    // handle the response
  });
}

The fact is that this controller can be attached to the view. And I want to make sure that the response is discarded, if the view has been deleted, this prevents conflicts with other requests that can be launched in other parts of the application. Will the controller instance be destroyed and with it, the services waiting for the call $httpwill be canceled if the view is deleted? For example, when a user moves (without reloading) from a page, causing Javascript to render a new section?

[ Edit ] I created jsfiddle , which shows that, at least for the $ timeout service, pending operations still work after they $scopeare destroyed during navigation. Is there an easy way to bind async operations to scope so that they are automatically destroyed?

+4
source share
1 answer

Attach a link to your promise first, then pass that link to the function cancel. This removes the promise with deviations. So you can just use promise.reject()insteadcancel(promise)

function MyController($scope, $http) {
  var promise =  $http.get('/service');

  promise.success(function(data){

  });

   $scope.$on(
         "$destroy",
         function() {
            promise.reject("scope destroyed, promise no longer available");
         }
   );
}
+2
source

All Articles