Angular/第三方模块是否支持网络流量监控,如何支持?
我想建立的是类似于 $watch 的东西,只是为了网络流量。
$scope.$watch('xhrTraffic', function(newval) {
newval - true when traffic starts/false when traffic ends
});
我希望能够控制浏览器何时处于网络使用状态以及何时不处于网络使用状态。
最佳答案
在我看来,你需要类似 this 的东西
<小时/>更新
此代码将有助于管理 XHR 请求:
(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 请求检查的示例。
有关拦截器的更多信息,请阅读 here
关于javascript - AngularJS:观察网络流量/xhr 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29644055/