首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AngularJS:监视网络流量/xhr请求

AngularJS:监视网络流量/xhr请求
EN

Stack Overflow用户
提问于 2015-04-15 07:26:38
回答 1查看 808关注 0票数 1

角/第三方模块对网络流量监控有支持吗?

我想要建立的是类似于$watch的东西,仅用于网络流量。

代码语言:javascript
复制
$scope.$watch('xhrTraffic', function(newval) {
    newval - true when traffic starts/false when traffic ends
});

我希望能够控制浏览器什么时候在使用网络,什么时候不使用。

EN

回答 1

Stack Overflow用户

发布于 2015-04-15 07:32:38

在我看来,你需要像这样的东西

更新

此代码将有助于管理XHR请求:

代码语言:javascript
复制
(function (){
'use strict';
angular.module( 'security.http-interceptor', [] )
    .factory( 'securityInterceptor', ['$injector', '$q', '$rootScope', function ( $injector, $q, $rootScope ){
                  return {
                      'request'       : function ( config ){
                          // do something on success
                          return config;
                      },
                      // optional method
                      'requestError'  : function ( rejection ){
                          // do something on error
                          if ( canRecover( rejection ) ) {
                              return responseOrNewPromise
                          }
                          return $q.reject( rejection );
                      },

                      // optional method
                      'response'      : function ( response ){
                          // do something on success
                          return response;
                      },

                      // optional method
                      'responseError' : function ( rejection ){
                          // do something on error
                          if ( canRecover( rejection ) ) {
                              return responseOrNewPromise
                          }
                          return $q.reject( rejection );
                      }
                  };
              }] )

    // We have to add the interceptor to the queue as a string because the interceptor depends upon service instances that are not available in the config block.
    .config( ['$httpProvider', function ( $httpProvider ){
                 $httpProvider.interceptors.push( 'securityInterceptor' );
             }] );
     }());

之前的链接是AUTH请求检查的一个例子。

有关拦截器的更多信息,请阅读这里

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29644055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档