簡體   English   中英

防止Angular $ httpProvider攔截器在模板加載時觸發

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

想要一個簡單的攔截器,該攔截器將在每200個狀態上觸發一種日志記錄方法。 這很容易設置,但是現在我注意到所有的Angular模板也都通過$ httpProvider加載,從而觸發了我的200攔截器。 有什么方法可以區分模板加載和實際API調用嗎?

  $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) {

                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