Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$q is not defined in function

Created a factory 'resInterceptor' and in that i am using functions(requestInterceptor,responseInterceptor) that is defined outside of factory. And it is giving error '$q is not defined' inside the function. But i want to do like this way only. Please suggest how to access $q inside requestInterceptor and responseInterceptor.

angular.module('resModule', ['ngResource', 'ngCookies'])
 .factory('resInterceptor', ['$rootScope', '$q', '$location', resInterceptor]);

 function resInterceptor($rootScope, $q, $location) {
    return {
        request: requestInterceptor,
        response: responseInterceptor,
    };
}

function requestInterceptor(config) {
   return config || $q.when(config); //$q is not defined
}

function responseInterceptor(response) {
   return response || $q.when(response);
}
like image 521
Neha Gupta Avatar asked Aug 05 '26 00:08

Neha Gupta


1 Answers

In order for this to work, you need to pass $q along explicitly and make requestInterceptor return your actual callback function:

function resInterceptor($rootScope, $q, $location) {
  return {
    request: requestInterceptor($q),
    ..
  };
}

function requestInterceptor($q) {
  return function (config) {
    return config || $q.when(config);
  };
}

Of course, this would be less complicated if you simply inlined the functions into the same scope where $q is defined in the first place.

like image 177
deceze Avatar answered Aug 06 '26 14:08

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!