2015-02-04 2 views
2

200 개 상태마다 로깅 메소드를 실행하는 간단한 인터셉터가 필요합니다. 이것은 설치하기 쉽지만, 이제는 모든 Angular 템플릿이 $ httpProvider를 통해로드되어 200 개의 인터셉터를 트리거한다는 것을 알게되었습니다. 템플릿로드와 실제 API 호출을 구별하는 방법은 무엇입니까?

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

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

답변

3

예. 할 수있어. 응답 객체에는 config 객체가 있으며 내부 구성 객체는 요청한 리소스의 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; 
      } 
     }; 
    }); 
+0

a-ha, 정말 고마워요. – diplosaurus

관련 문제