Javascript 防止$httpProvider拦截器在模板加载时触发
想要一个简单的拦截器,每200个状态触发一个日志记录方法。这很容易设置,但现在我注意到我所有的Angle模板都是通过$httpProvider加载的,因此触发了我的200拦截器。有没有办法区分模板加载和实际API调用Javascript 防止$httpProvider拦截器在模板加载时触发,javascript,angularjs,angular-promise,Javascript,Angularjs,Angular Promise,想要一个简单的拦截器,每200个状态触发一个日志记录方法。这很容易设置,但现在我注意到我所有的Angle模板都是通过$httpProvider加载的,因此触发了我的200拦截器。有没有办法区分模板加载和实际API调用 $httpProvider.interceptors.push(function() { return { response: function(response) { if(response.status ===
$httpProvider.interceptors.push(function() {
return {
response: function(response) {
if(response.status === 200) console.log(response);
return response;
}
};
});
对。你可以。在响应对象中,有一个配置对象,在配置对象中是您请求的资源的URL。所以
$httpProvider.interceptors.push(function() {
return {
response: function(response) {
if(response.status === 200) console.log(response);
return response;
}
};
});
$httpProvider.interceptors.push(function() {
return {
response: function(response) {
function endsWith ( str, suffix ) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
if(response.status === 200 && !endsWith( response.config.url, '.html') ) console.log(response);
return response;
}
};
});