user3174311
user3174311

Reputation: 1991

How to cancel a $resource request in AngularJS

I have a ajax call like this

    angular.module('my.module')
    .factory('Users', ['config', '$resource', function(config, $resource) {
      return $resource('/my/endpoint', {
        userId: '@userId'
      }, {
        query: {
            method: "GET"
        }
    });
  }])

then I have a UserLoader

.factory('UserLoader', ['Users', 'LoaderFactory', function(users, LoaderFactory) {
   [some other stuff]
}])

and the the controller

.controller('UserSearchCtrl', ['$scope', 'UserLoader', function($scope, UserLoader) {

}])

how can I cancel a previous call before to send another?

Upvotes: 1

Views: 423

Answers (1)

Andrey
Andrey

Reputation: 4050

You need to configure your resource as cancellable

query: {method: 'get',cancellable: true}

Then you can call $cancelRequest() on this resource.

Users.query().$cancelRequest()

Upvotes: 1

Related Questions