0

Is that possible in some way to intercept and handle http responses being outside Angular? I can't use $httpProvider because target script is loaded after Angular has been initialized. So I need something that would work like ajaxSuccess in jQuery.

accme
  • 383
  • 1
  • 2
  • 11
  • If the outside script is using jQuery you can add an interceptor for jQuery from inside Angularjs. – Reactgular Sep 24 '15 at 18:53
  • No, jQuery is not available here – accme Sep 24 '15 at 19:01
  • You would need to somehow hook into angular if you want to intercept http requests sent by angular. you can't intercept http requests normally. The only reason you would be able to with jQuery sent or angular sent http requests is because each provides hooks that you can attach events to. – Kevin B Sep 24 '15 at 19:24
  • you would need to overload `XMLHttpRequest` object itself before other library loads. Keep in mind that `$http` will also use it too – charlietfl Sep 24 '15 at 19:24
  • See this overload example http://stackoverflow.com/questions/629671/how-can-i-intercept-xmlhttprequests-from-a-greasemonkey-script or http://stackoverflow.com/questions/16959359/intercept-xmlhttprequest-and-modify-responsetext – charlietfl Sep 24 '15 at 19:31

1 Answers1

1

It is rarely recommended to do that because it is un-Angular and usually indicates poor app design. But nothing stops the one from using Angular features outside of the app.

angular.module('httpModule', ['ng'], function ($httpProvider) {
  $httpProvider.interceptors.push(...);
});

var http = angular.injector(['httpModule']).get('$http');
Estus Flask
  • 206,104
  • 70
  • 425
  • 565