正确的方法来禁用/删除HTTP角拦截? [英] Right way to disable/remove http interceptors in Angular?

查看:178
本文介绍了正确的方法来禁用/删除HTTP角拦截?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着这个职位实行一个项目一个类似的ajax装载机图片:

我的实现有一些差异:


  • 我用 $ rootScope 发出,而不是广播我也用 $ rootScope 的指令来处理该事件。

  • 由于项目的特殊性,我不得不取消绑定指令 $ rootScope。$关于监听器被触发的第一个事件之后(无论是显示还是隐藏) ,在事件处理中。

  • 我只火了一个显示/隐藏事件。展会上的第一个HTTP请求,隐藏当计数达到0。

我相信这些都是从链接后的主要区别,不知道他们是有关我的问题,但万一他们是...

在装载机hide事件的处理,装载机不见了,除非页面被刷新,我不会再次显示它,但我仍然有背景的http请求刷新当前页面上的数据。这些请求仍然会被拦截,并触发新的显示/隐藏事件,不再需要/处理。我只需要一个首秀并首先隐藏,仅此而已。

什么是删除我添加到 $ httpProvider 后的第一个隐藏事件已经被解雇?

我知道我们添加使用拦截器 $ httpProvider.interceptors.push(),但我不知道怎么突然出来时,我不再需要该拦截器


解决方案

我打算把一个赏金这一点,因为我有同样的问题。但是....它看起来好像拦截 responseInterceptors 只是阵列,根据的来源$ C ​​$ C (线127和133 $ httpProvider 厂)。有没有包装解决这个问题。

据我所知,你要么必须使用 POP()或任何其他阵列的方法。但是,这意味着,你不知道你在什么大跌眼镜!持有该对象的引用,也不会真正帮助,因为你不能真正做到一个阵列的功能就可以了,除非你决定基于平等(这可以工作,使用的indexOf或别的类似下划线)进行迭代。

真的,什么角度的需求是这个包装,因为你不能确定你的拦截器是名单上的最后一个。

I followed this post to implement a similar ajax loader image on a project:

My implementation has a few differences:

  • I use $rootScope to emit and not broadcast and I also use $rootScope on the directive to handle the event.
  • Because of a particularity of the project, I have to unbind the directive $rootScope.$on listeners right after the first event being fired (either for show or hide), inside the event handler.
  • I only fire a single show/hide event. Show on the first HTTP request, hide when the count reaches 0.

I believe those are the major differences from the linked post, not sure if they are relevant to my question but just in case they are...

When the loader hide event is handled, the loader is gone and I won't show it again unless the page is refreshed, but I still have background http requests to refresh data on the current page. Those requests will still be intercepted and fire new show/hide events, which are no longer required/handled. I only need a first show and first hide, that's it.

What's the right way to remove the HTTP interceptor I added to the $httpProvider after the the first hide event has been fired?

I know we add the interceptor using a $httpProvider.interceptors.push() but I'm not sure how to pop it out when I no longer need that interceptor.

解决方案

I was going to put a bounty on this, as I had the same question. However....it looks as though the interceptors and responseInterceptors are simply arrays, according to the source code (lines 127 and 133 in the $httpProvider factory). There's no wrapper around this.

From what I can tell, you would either have to use pop() or any other array method. However, this means that you don't know what you're popping! Holding a reference to the object wouldn't really help, because you can't really do an array function on it, unless you decide to iterate based on equality (which could work, using indexOf or something else like Underscore).

Really, what Angular needs is a wrapper for this since you can't be sure that your interceptor is the last one on the list.

这篇关于正确的方法来禁用/删除HTTP角拦截?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆