Is there a way to request $http for an interceptor?

84 views
Skip to first unread message

Evgeni T

unread,
Jan 14, 2014, 11:39:54 AM1/14/14
to ang...@googlegroups.com
The idea is to get a data from another source in certain cases, so I have this:

factory("interceptor", function ($q, $location, $http) {
    return function (promise) {
        return promise;
    }
}

which fails with [$injector:cdep] Circular dependency found: interceptor <- $http

Also tried to inject $injector and retrieve $http using that, with same results. Any ideas?

Ben Greer

unread,
Jan 14, 2014, 4:54:02 PM1/14/14
to ang...@googlegroups.com
why would you want an interceptor to depend on $http?

dude, your doing it wrong

phindmarsh

unread,
Jan 15, 2014, 12:23:04 AM1/15/14
to ang...@googlegroups.com
AFAIK you do need to use the $injector service, but only ask for $http when the interceptor is called (i.e. in your callback function), so in your example:

factory("interceptor", function ($q, $location, $injector) {
    var $http;
    return function (promise) {
        $http = $http || $injector.get('$http'); // do it here instead
        // do something with $http here...
        return promise;
    }
}

I've used this approach before when depending on services in my interceptors that depend on $http, seems to do the trick.

Evgeni T

unread,
Jan 15, 2014, 6:48:25 AM1/15/14
to ang...@googlegroups.com
I'll try that.
I'm trying to figure out a better way to get a mock data for e2e testing, $httpBackend doesn't seem to do what I want it to. I'm still figuring it out, though.


--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/dTYSBPoQHN8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Evgeni T

unread,
Jan 19, 2014, 9:31:44 PM1/19/14
to ang...@googlegroups.com
Yeah, it works, thanks. Turned out I didn't really need it, though.. but might need it in a future.
Wrote a blog post on e2e testing, which explains why I wanted this.
Reply all
Reply to author
Forward
0 new messages