Angular JS cancel $http call before calling a new

2020-07-16 08:28发布

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条回答
Juvenile、少年°
2楼-- · 2020-07-16 09:06

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.

查看更多
登录 后发表回答