Angular JS cancel $http call before calling a new

2020-07-16 08:47发布

问题:

In angular JS 1.1.5 you can cancel previously started $http calls. These two link1 and link2 gives some explanation on how it works.

However I can't understand, how to use this in practice. I created plnkr to illustrate what I want to achieve.

  1. User has a link he can click on (as we know, users tend to do that very passionately)
  2. If user clicks link, and previous request hasn't yet finished, it should be canceled, and new request should be made (in this example there are other ways to ignore multiple clicks. Original problem is user input field with ng-change on it - request gets made every time user changes value - every keypress will fire $http. I'm using link to make it a bit simpler)

As you can see in plnkr request fires and gets canceled immediately, but I'm expecting last request to succeed.

I'm new to angularjs so probably I'm doing it wrong. I've gooled everything I can imagine, but there are no full examples for canceler.resolve()

Can anyone help me with this or guide me to right direction?

回答1:

You need to create a new canceler for each request, because once it's resolved it can't be used again.

See here for an updated plnkr.