AngularJS: need to fire event every time an ajax call is started

后端 未结 4 1635
我在风中等你
我在风中等你 2020-12-13 10:33

I am trying to fire an event on $rootScope every time an ajax call is started.

var App = angular.module(\'MyApp\');

App.config(function ($httpProvider) {
           


        
4条回答
  •  有刺的猬
    2020-12-13 11:21

    I have verified that this code will work as you expect. As I mentioned above, you are not retrieving the injector that you think you are and need to retrieve the one being used for your app.

    discussionApp.config(function($httpProvider) {
      $httpProvider.defaults.transformRequest.push(function(data) {
        var $injector, $log, $rootScope;
        $injector = angular.element('#someid').injector();
    
        $rootScope = $injector.get('$rootScope');
        $rootScope.$broadcast('httpCallStarted');
    
        $log = $injector.get('$log');
        $log.log('httpCallStarted');
        return data;
      });
    });
    

提交回复
热议问题