简体   繁体   English

防止Angular $ httpProvider拦截器在模板加载时触发

[英]Prevent Angular $httpProvider interceptor from firing on template loads

Want a simple interceptor that will fire a logging method on every 200 status. 想要一个简单的拦截器,该拦截器将在每200个状态上触发一种日志记录方法。 This was easy to setup, but now I'm noticing that all of my Angular templates are loaded via $httpProvider as well and thus triggering my 200 interceptor. 这很容易设置,但是现在我注意到所有的Angular模板也都通过$ httpProvider加载,从而触发了我的200拦截器。 Any way to differentiate between template loads and actual API calls? 有什么方法可以区分模板加载和实际API调用吗?

  $httpProvider.interceptors.push(function() {
      return {
          response: function(response) {

              if(response.status === 200) console.log(response);
              return response;
          }
      };
  });

Yes. 是。 You can. 您可以。 Within the response object, there is a config object and inside config object is the URL of the resource you had requested. 在响应对象中,有一个配置对象,而配置对象内部是您请求的资源的URL。 So, 所以,

 $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;
          }
      };
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM