AngularJS $资源拦截器 [英] AngularJS $resource interceptors

查看:141
本文介绍了AngularJS $资源拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将拦截器添加到 $ resource 调用?

How do I add interceptors to a $resource call?

假设我有一个名为的资源工厂用户,就像这样;

Let's say I have a resource factory called Users, like so;

app.factory('Users', ['$resource', 'resourceInterceptor',
  function ($resource, resourceInterceptor) {
    return $resource(
      'users/:user_id',
      {
        user_id: '@id'
      },
      {
        query: {
          method: 'GET', // Not changing the default method, just adding an interceptor
          interceptor: resourceInterceptor // Is there any better way to do this.. like globally?
        },
        save: {
          method: 'POST', // Same here
          interceptor: resourceInterceptor // Again...
        },
        ..., // And so on
      }
    );
  }]);

和我的 resourceInterceptor 服务看起来像;

and my resourceInterceptor service looks like;

app.factory('resourceInterceptor', ['$rootScope',
  function ($rootScope) {
    return {
      request: function () {
        // This function isn't executed at all?
        $rootScope.loading = true;
      },
      response: function () {
        $rootScope.loading = false;
      },
      responseError: function () {
        $rootScope.loading = false;
      }
    };
  }]);

首先,请求拦截功能永远不会被执行,为什么不呢?

First of all, the request intercept function is never executed, why not?

其次,必须将拦截器硬编码到现有的 $ resource 方法是非常繁琐的,有没有办法更轻松地将拦截器分配给特定的 $ resource 调用,或者甚至可以为所有 $ resource 调用?

Secondly, having to hardcode the interceptor to existing $resource methods is very tedious , is there a way to easier assign interceptors to specific $resource calls, or maybe even assign an interceptor to all $resource calls?

推荐答案

要在资源中使用拦截器,您应该:

To use an interceptor in a resource you should:

1 - 使用request,response,responseError创建一个httpInterceptor:

1 - Make an httpInterceptor with you request, response, responseError:

app.factory('myInterceptor', function () {
    //Code
    //return { request:...,
});

2 - 在你的应用中配置这个拦截器:

2 - Config this Interceptor in your app:

app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('myInterceptor');
}]);

现在你已经配置你的httpProvider有一个拦截器无论你注入$ http你将使用这个提供者所以...你将执行你的请求,响应和responseError函数。

Right now as you have config your httpProvider to has an interceptor wherever you inject $http you will use this provider so... you will excute your request, response and responseError funciton.

3 - 在资源中使用它。
由于$资源使用$ http并且你配置了一个httpProvider globaly,你将在你使用你的资源时调用你的拦截器的函数。

3 - Using it in a resource. As $resource use $http and you have config a httpProvider globaly you will call your interceptors' func when you use your resource.

第二个问题:
你不能将拦截器设置为具体的$ http对象,它们(拦截器)是全局设置的。

Second question: You can not set an interceptor to a concrete $http object, they (interceptors) are set globally.

(即使你在模块定义之前设置了拦截器,然后你删除它,你不知道执行顺序)

(Even if you set the interceptor before your module definition and then you remove it, you can not know the execution order)

如果你不想覆盖每个$ resource动作中的拦截器属性你可以做什么(就像你一样)写在你的问题中)你可以改进你的拦截器。

What you can do if you do not want to override the interceptor property in each $resource action (as you write in your question) you can improve your interceptor.

app.factory('userLoadingInterceptor', function () {
    //Code
    return {
        request: function(){
            //Check if your are working with a url related with users
            // and if so do things...
        }
});

这篇关于AngularJS $资源拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆