AngularJS - Put $http interceptor in separate file -


how can put interceptor in separate file , push in $httpprovider within configuration block.

here's configuration block @ moment:

(function() {   'use strict';    angular     .module('app')     .config(config);    config.$inject = ['$httpprovider'];    function config($httpprovider) {     $httpprovider.defaults.withcredentials = true;     $httpprovider.defaults.usexdomain = true;     $httpprovider.interceptors.push(['$injector', '$q', function($injector, $q) {       return {         responseerror: function(response) {           var toastr = $injector.get('toastr');           var lodash = $injector.get('lodash');           toastr.error(lodash.get(response, 'data.message', 'alguma coisa deu errado.'));           if (response.status !== 401) {             return $q.reject(response);           }            var userservice = $injector.get('userservice');           if (userservice.islogged()) {             userservice.logout();           }            var $state = $injector.get('$state');           $state.go('login');           return $q.reject(response);         }       };     }]);   } })(); 

create factory using code inside return {.....} , push factory intercepters collection.

angular .module('app') .config(['$httpprovider', function($httpprovider) {
$httpprovider.interceptors.push('yourinterceptorfactoryname'); }]);


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -