Angular JS cancel $http call before calling a new $http

谁说我不能喝 提交于 2019-12-06 03:47:02

问题


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.



来源:https://stackoverflow.com/questions/17404579/angular-js-cancel-http-call-before-calling-a-new-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!