angularjs $httpProvider 拦截器文档 [英] angularjs $httpProvider interceptor documentation

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

问题描述

我是角度(和编程)的新手,这是一个看似简单的问题,但我无法弄清楚.

I am new to angular (and programming), here is a seemingly simple question but I could not figure it out.

一些教程建议使用 $httpProvider.interceptors.push('interceptorName') 来操作 http 请求和响应.

some tutorials suggests using $httpProvider.interceptors.push('interceptorName') to manipulate the http request and response.

我想了解更多关于拦截器的事情,所以我查看了官方文档,但我找不到与拦截器相关的任何内容,只有一个方法 (useApplyAsync([value]);) 和一个属性(默认值)在 $httpProvider(docs)中.

I want to know more about the interceptor thing so I look at the official document, but I could not find anything related to interceptor, there are only a method (useApplyAsync([value]);) and a property (defaults) in $httpProvider (docs).

我从其他教程中知道拦截器是一个常规的服务工厂,我知道如何使用它,但我的问题是:因为语法是 $httpProvider.interceptors.push('interceptorName'),然后我希望我会在 $httpProvider 中找到一个名为拦截器"的属性,但实际上我不能.我想念这种混乱吗?还是我的概念从根本上是完全错误的?

I know from other tutorials that an interceptor is a regular service factory and I know how to use it, but my question is: since the syntax is $httpProvider.interceptors.push('interceptorName'), then I expect I will find a property called "interceptors" in $httpProvider, but in fact I can't. Is something I miss to get this confusion? or is my concept totally wrong from the bottom?

推荐答案

拦截器位于 此处的文档.

Interceptors are in the documentation here.

这是一个如何写一个的例子.

Here's an example of how to write one.

.config([
  '$httpProvider',
  function($httpProvider) {

    var interceptor = [
      '$q',
      '$rootScope',
      'userSession',
      function($q, $rootScope, userSession) {

        var service = {

          // run this function before making requests
          'request': function(config) {

            if (config.method === 'GET' || userSession.isAuth()) {
              // the request looks good, so return the config
              return config;
            }

            // bad request, so reject
            return $q.reject(config);

          }

        };

        return service;

      }
    ];

    $httpProvider.interceptors.push(interceptor);

  }
])

$httpProvider 文档页面上没有关于拦截器的原因是因为开发人员没有在 $http 生成文档的脚本:

The reason there's nothing on the $httpProvider documentation page about interceptors is because the developers didn't include the following code in the $http script which the docs are generated from:

/**
   * @ngdoc property
   * @name $httpProvider#interceptors
   * @description
// etc

众所周知,文档通常不完整、不准确和/或令人困惑.直到最近,当我无法找到或理解某些东西时,我一直认为是我的问题,但我发现这通常是因为文档太糟糕了.然而,我们都应该感谢我们有如此出色的工具可供使用,并记住,也许文档很差,因为时间必须集中在编写工具而不是工具手册上.

Documentation in general is known to be incomplete, inaccurate, and/or confusing. Until recently, I always thought I was the problem when I couldn't find or understand something, but I've found that it's often because documentation is just lousy. However, we should all be thankful that we have such great tools to use and keep in mind that perhaps the documentation is poor because time had to be focused on writing the tool instead of the manual for the tool.

最可靠的文档"是源代码本身,尽管阅读起来可能不太友好!在我上面链接的源代码中,您可以看到 this.interceptors = [].this 指的是 $httpProvider,因此它正在将属性 interceptors 分配给 $httpProvider,其值为一个空数组.要添加拦截器,您只需将拦截器 push() 加入该数组即可.

The most reliable "documentation" is the source code itself, though it can be a lot less friendly to read! In the source code I linked above, you can see this.interceptors = []. this refers to the $httpProvider, so it is assigning the property interceptors to $httpProvider with the value being an empty array. To add your interceptors, you simply push() your interceptor to this array.

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

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