AngularJs : event listener on http requests

标签 angularjs ionic-framework

由于我使用 Oauth2 来保护我的 Api,因此如果之前的访问 token 已过期,我需要在任何 http 请求之前获取新的访问 token 。

直到现在我还没有太多使用事件监听器。

这是我现在所做的(请告诉我它是否正确):

ApplicationController.js:

app.controller('ApplicationController', function($rootScope, $scope, $localStorage, AuthService){

    // Listening event apiRequested
    $scope.$on('event:apiRequested', function(e) {

        AuthService.token();
        // Restore the access_token in case it has changed
        access_token = $localStorage.getObject('access_token');

    });

})

UserController.js:

$rootScope.$broadcast('event:apiRequested');

// Get Users around
return $http.post(domain+'/api/users?access_token='+access_token.key, data).then(function(response){
    return response;
});

首先我不确定...如果事件已经完全执行,$http 是否会被处理?

因此,由于我不确定,我正在考虑添加回调。

这里的想法:

$rootScope.$broadcast('event:apiRequested', function(response){

    if(response){

        // Get Users around
        return $http.post(domain+'/api/users?access_token='+access_token.key, data).then(function(response){
            return response;
        });

    }

});

请告诉我是否可以这样做,或者我应该在这种情况下使用事件监听器之外的其他内容。

最佳答案

为什么不使用 interceptors那是为了拦截HTTP请求吗? 在您的情况下,您应该将这个非常具体的行为添加到“请求”部分中。

参见下面的拦截器示例:

var $myService; // Add a constant that store the service
    $httpProvider.interceptors.push(['$location', '$injector', '$q', function($location, $injector, $q) {
                             return {

                               'request' : function(config){
                                 console.log("intercept request", config.url,config)
                                 // Your token shall be retreive in this part
                                 return config
                               },
                               'response' : function(config){
                                 $myService= $myService|| $injector.get('$myService'); // inject the service manually if constant is undefined
                                 console.log("intercept response", config)
                                 // Your token shall be retreive in this part
                                 return config
                               },
                                 'responseError': function(rejection) {

                                     console.log("responseError intercepted" , rejection);
                                      if (rejection.status === 403) {

                                         return $q.reject(rejection);

                                     } else if (rejection.status === 423) {



                                         return $q.reject(rejection);
                                     }else
                                          return $q.reject(rejection);
                                 }
                             };
                         }]);

拦截器应定义在 .config(["$httpProvider", function($httpProvider)

关于AngularJs : event listener on http requests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30592628/

相关文章:

ios - 如何将 Autodesk Forge Viewer 嵌入移动应用程序?

javascript - 无法在 iOS 设备中安装 ionic 应用程序

javascript - AngularJs 用户身份验证仅发生在第二次交互时

javascript - 使用不同的 ng-controller 和 ng-include

ionic-framework - Ionic-Vue Ionicons 5.x.x 不显示图标

javascript - ionic 框架中带有禁用复选框的 ionic 列表

ios - Ionic App : Received memory warning. 来自调试器的消息:由于内存问题而终止

javascript - 服务的 Angular 解决 promise

javascript - 减少 Angular JS 实时 ajax 请求的消耗

javascript - 未提供过滤器时 Angular 显示全部