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

查看:173
本文介绍了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([值]);)和属性(默认)在 $ httpProvider 文档)。

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 文档页面上是因为开发商没有包括在以下code中的<一个href=\"https://github.com/angular/angular.js/blob/master/src/ng/http.js#L178\"><$c$c>$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.

最可靠的文件是源$ C ​​$ C本身,虽然它可以少了很多友好的阅读!在源$ C ​​$ C我上面链接,就可以看到 this.interceptors = [] 这个 $ httpProvider ,因此它被分配财产拦截 $ httpProvider 的值是一个空数组。要添加拦截器,你只需推()您拦截此阵。

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天全站免登陆