Devin Dixon
Devin Dixon

Reputation: 12403

AngularJS explicit annotation and cannot be invoked in strict mode

I am using ng-strict-di to uncover dependency injection problems. I am getting a kinda weird error in Angular 1.6 when I am trying to intercept the $httpd. My code is this:

app.config(['$httpProvider',
function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = true;

    var access_token = 'abc123';

    $httpProvider.interceptors.push(function($q) {
        return {
            'request' : function(config) {

                if(config.url.indexOf('htm', config.url.length - 3) === -1 && config.url.indexOf('html', config.url.length - 4) === -1) {
                    config.url = config.url + '?access_token=' + encodeURIComponent(access_token);
                }

                return config;
            }
        };
    });
}]);

The error that is happening is this:

Uncaught Error: [$injector:strictdi] function($q) is not using explicit annotation and cannot be invoked in strict mode
http://errors.angularjs.org/1.6.1/$injector/strictdi?p0=function(%24q)
    at angular-1.4.3.js:68
    at Function.annotate [as $$annotate] (angular-1.4.3.js:4072)
    at injectionArgs (angular-1.4.3.js:4799)
    at Object.invoke (angular-1.4.3.js:4834)
    at angular-1.4.3.js:11241
    at forEach (angular-1.4.3.js:357)
    at $HttpProvider.$get (angular-1.4.3.js:11239)
    at Object.invoke (angular-1.4.3.js:4842)
    at angular-1.4.3.js:4636
    at getService (angular-1.4.3.js:4783)

From my understanding, $q should not have to be defined because the function is defined. Why is the $q causing an error and how can a fix this?

Upvotes: 0

Views: 3996

Answers (1)

Tonio
Tonio

Reputation: 5014

I think you just need to "declare" your dependency with $q like this:

app.config(['$httpProvider', '$q'
function($httpProvider, $q) {
    $httpProvider.defaults.useXDomain = true;

With this, you will be able to use $q anywhere in your config (and so in your http interceptor)

Upvotes: 1

Related Questions