简体   繁体   中英

Prevent Angular $httpProvider interceptor from firing on template loads

Want a simple interceptor that will fire a logging method on every 200 status. 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. Any way to differentiate between template loads and actual API calls?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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