本文介绍了AngularJS服务相互依存通过身份验证令牌HTTP头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了三个服务,OpenAPI的,SecureApi和TokenHandler。我想从SecureApi服务使用TokenHandler服务。认证时会发生控制器code将在TokenHandler.Set设置令牌()方法。我想SecureApi到CAL tokenHandler.get(),如下。可能吗。现在我得到以下错误: -

编辑: - 修正错误以下(更新code下面过,但我的令牌仍然没有得到通过作为头的一部分:)(

未知提供商:tokenHandlerProvider< - tokenHandler< - SecureApi

code: -

  / *服务* /
angular.module('MyApp.services',['ngResource'])
    .factory('OpenAPI的',函数($资源){        VAR的OpenAPI = $资源(
            '/ API /:控制器/:身份证',
            []
            {
                postLogOn:{方法:POST,则params:{控制器:'账户'}},
                postCustomer:{方法:POST,则params:{控制器:'员工'}}
            }
        );
        返回的OpenAPI;
    }).factory('TokenHandler',函数(){
    变种tokenHandler = {};
    VAR令牌=无;    tokenHandler.set =功能(newToken){
        令牌= newToken;
    };    tokenHandler.get =功能(){
        返回记号。
    };
    返回tokenHandler;
})
   .factory('SecureApi',['$资源','TokenHandler'功能(RES,tokHandler){
       VAR secureApi = $资源(
        '/ API /:控制器/:身份证',
        []
        {
    getInsightCustomer:{方法:GET,则params:{控制器:'MyCustomer'},标题:{Authorization_Token:tokenHandler.get()}}
        }
    );
       返回secureApi;
   }]);


解决方案

我得到了这个答案在AngularJS Google网上论坛: -

这是上面code语法错误。

您TokenHandler服务有一个大写字母t,但你SecureApi服务请求tokenHandler以小写吨。一般来说是更好 - 必不可少的,如果你要尽量减少你的js使用数组注射格式,这也再允许你使用任何你喜欢作为该服务的参数名称。例如:

.factory('SecureApi',['$资源','TokenHandler'功能(RES,tokHandler){
 ...
}]);

I have created three services, OpenApi, SecureApi, and TokenHandler. I want to use TokenHandler Service from SecureApi Service. When authentication will happen controller code will set token in TokenHandler.Set() method. and I want SecureApi to cal tokenHandler.get() as below. Is it Possible. Right now I am getting below error:-

EDIT:- fixed below error (updated the code below too, but my token is still not getting through as part of header :()

Unknown provider: tokenHandlerProvider <- tokenHandler <- SecureApi

Code:-

/* Services */
angular.module('MyApp.services', ['ngResource'])
    .factory('OpenApi', function ($resource) {

        var openApi = $resource(
            '/api/:controller/:id',
            [],
            {
                postLogOn: { method: 'POST', params: { controller: 'Account' } },
                postCustomer: { method: 'POST', params: { controller: 'Employee' } }
            }
        );
        return openApi;
    })

.factory('TokenHandler', function () {
    var tokenHandler = {};
    var token = "none";

    tokenHandler.set = function (newToken) {
        token = newToken;
    };

    tokenHandler.get = function () {
        return token;
    };
    return tokenHandler;
})


   .factory('SecureApi', ['$resource', 'TokenHandler', function(res, tokHandler) { 
       var secureApi = $resource(
        '/api/:controller/:id',
        [],
        {
    getInsightCustomer: { method: 'GET', params: { controller: 'MyCustomer' }, headers: {Authorization_Token: tokenHandler.get()} }
        }
    );
       return secureApi;
   }]);
解决方案

I got answer of this in AngularJS googlegroups:-

https://groups.google.com/forum/?hl=en&fromgroups=#!topic/angular/jfttVnvga9M

It was a syntax error in above code.

You TokenHandler service has an upper case T but your SecureApi service is "requesting" tokenHandler with a lower case t. Generally it is better - and essential if you are going to minimize you js to use the array injection format, which also then allows you to use anything you like as a parameter name for that service. For example:

.factory('SecureApi', ['$resource', 'TokenHandler', function(res, tokHandler) { ...}]);

这篇关于AngularJS服务相互依存通过身份验证令牌HTTP头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 19:53