AngularJS:每次启动ajax调用时都需要触发事件

bfc*_*ara 16 angularjs

每次启动ajax调用时,我都试图在$ rootScope上触发一个事件.

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

App.config(function ($httpProvider) {
    //add a transformRequest to preprocess request
    $httpProvider.defaults.transformRequest.push(function () {
        //resolving $rootScope manually since it's not possible to resolve instances in config blocks
        var $rootScope = angular.injector(['ng']).get('$rootScope');
        $rootScope.$broadcast('httpCallStarted');

       var $log = angular.injector(['ng']).get('$log');
       $log.log('httpCallStarted');
    });
});
Run Code Online (Sandbox Code Playgroud)

事件'httpCallStarted'它没有被解雇.我怀疑在配置块中使用$ rootScope或任何其他实例服务是不正确的.如果是这样,我怎样才能在每次启动http调用时获取事件,而不必在每次拨打电话时都传递配置对象?

提前致谢

Ben*_*esh 17

你总是可以在服务中包装$ http.由于服务只设置一次,您可以让服务工厂为您设置事件.老实说,这对我来说感觉有些神圣,但这是一个很好的解决方法,因为Angular还没有全球性的方法来做到这一点,除非在1.0.3中添加了一些我不知道的东西.

这是一个工作的吸收者

这是代码:

app.factory('httpPreConfig', ['$http', '$rootScope', function($http, $rootScope) {
    $http.defaults.transformRequest.push(function (data) {
        $rootScope.$broadcast('httpCallStarted');
        return data;
    });
    $http.defaults.transformResponse.push(function(data){ 
        $rootScope.$broadcast('httpCallStopped');
        return data;
    })
    return $http;
}]);

app.controller('MainCtrl', function($scope, httpPreConfig) {
  $scope.status = [];

  $scope.$on('httpCallStarted', function(e) {
    $scope.status.push('started');
  });
  $scope.$on('httpCallStopped', function(e) {
    $scope.status.push('stopped');
  });

  $scope.sendGet = function (){ 
    httpPreConfig.get('test.json');    
  };
});
Run Code Online (Sandbox Code Playgroud)


Rya*_*ill 12

我已经验证此代码可以按预期工作.正如我上面提到的,您没有检索到您认为自己的注射器,需要检索用于您的应用的注射器.

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;
  });
});
Run Code Online (Sandbox Code Playgroud)


Onu*_*rım 12

Cagatay是对的.更好地使用$http拦截器:

app.config(function ($httpProvider, $provide) {
    $provide.factory('httpInterceptor', function ($q, $rootScope) {
        return {
            'request': function (config) {
                // intercept and change config: e.g. change the URL
                // config.url += '?nocache=' + (new Date()).getTime();
                // broadcasting 'httpRequest' event
                $rootScope.$broadcast('httpRequest', config);
                return config || $q.when(config);
            },
            'response': function (response) {
                // we can intercept and change response here...
                // broadcasting 'httpResponse' event
                $rootScope.$broadcast('httpResponse', response);
                return response || $q.when(response);
            },
            'requestError': function (rejection) {
                // broadcasting 'httpRequestError' event
                $rootScope.$broadcast('httpRequestError', rejection);
                return $q.reject(rejection);
            },
            'responseError': function (rejection) {
                // broadcasting 'httpResponseError' event
                $rootScope.$broadcast('httpResponseError', rejection);
                return $q.reject(rejection);
            }
        };
    });
    $httpProvider.interceptors.push('httpInterceptor');
});
Run Code Online (Sandbox Code Playgroud)

我认为interceptors适用于1.1.x之后的版本.有responseInterceptors该版本之前.

  • 这是一个非常好的答案.我不得不在拦截器中使用一个服务(显示/隐藏微调器),但是我使用了.config来显示它,并且不可能在.config中使用服务,现在使用这个解决方案一切顺利而且很好! (2认同)