angularjs - Why does my interceptor that is defined on two modules get run twice? -


why here logged out twice?

how can avoid being logged out twice, while still using factory module? (i want because makes testing easier)

code:

angular   .module('app', ['factory'])   .controller('maincontroller', maincontroller)   .factory('httpinterceptorfactory', httpinterceptorfactory)   .config(config) ;  angular   .module('factory', [])   .factory('factory', factory)   .factory('httpinterceptorfactory', httpinterceptorfactory)   .config(config) ;  function maincontroller(factory) {   var vm = this;   vm.sendrequest = function() {     factory.sendrequest();   }; }  function factory($http) {   return {     sendrequest: function() {       $http.get('http://jsonplaceholder.typicode.com/users')     }   }; }  function httpinterceptorfactory() {   return {     request: function(config) {       console.log('here');       return config;     }   }; }  function config($httpprovider) {   $httpprovider.interceptors.push('httpinterceptorfactory'); } 

plnkr - http://plnkr.co/edit/9nmpvb2jvneud9klfmq4?p=preview

you including same config twice each push same interceptor interceptors array , therefore fire 2 instances per request.

keep in mind components available throughout app regardless of module declared in.

so including factory once make available anywhere

edit: after re-reading appears want able include in both modules testing.

if want both config test if interceptor in array before pushing duplicate.

function config($httpprovider) {    if($httpprovider.interceptors.indexof('httpinterceptorfactory') ===-1){        $httpprovider.interceptors.push('httpinterceptorfactory');   } } 

demo


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -