Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何进入工厂';s函数与角函数中的其他函数_Javascript_Angularjs_Jwt - Fatal编程技术网

Javascript 如何进入工厂';s函数与角函数中的其他函数

Javascript 如何进入工厂';s函数与角函数中的其他函数,javascript,angularjs,jwt,Javascript,Angularjs,Jwt,我真的在试图弄清楚这一切是如何运作的。我觉得这应该行得通。我有一个Auth工厂,当jwt过期时,它调用它的方法'delegate',该方法获得一个带有刷新令牌的新令牌。由于某种原因,我得到错误“\u this.delegate不是函数”。(出于测试目的,我在这两种情况下都进行委托) 您只需从\u this.delegate()中删除\u this 函数delegate()在您使用它的范围内 在工厂中使用var没有什么问题。服务是不同的在isAuthenticated函数中没有delegate()

我真的在试图弄清楚这一切是如何运作的。我觉得这应该行得通。我有一个Auth工厂,当jwt过期时,它调用它的方法'delegate',该方法获得一个带有刷新令牌的新令牌。由于某种原因,我得到错误“\u this.delegate不是函数”。(出于测试目的,我在这两种情况下都进行委托)


您只需从
\u this.delegate()
中删除
\u this

函数
delegate()
在您使用它的范围内


工厂中使用
var
没有什么问题。
服务
是不同的

isAuthenticated
函数中没有
delegate()
,因此
这个.delegate()
将是未定义的,因为闭包在JavaScript中是如何工作的。@Claies ok,所以我事先添加了对工厂的引用,但仍然看到相同的错误:
webapp.factory('Auth',function($http,API\U URL,$window,$location,jwtHelper){var\u this=this;return{…\u this.delegate();…
triggers\u this.delegate()不是函数。我更改了示例代码以反映这一点。
webapp.factory('Auth', function($http, API_URL, $window, $location, jwtHelper ) {
var _this = this;
    var delegate = function(){
        $http.post(API_URL+'/admin/delegate', {refresh_token: $window.sessionStorage.refreshToken } ).success(function(result) {
            $window.sessionStorage.authToken = result.token;
            $window.sessionStorage.refreshToken = result.refresh_token;
            console.log('delegate-result: '+JSON.stringify(result));
            $location.path('/about');
            //LocalService.set('authToken', JSON.stringify(result));
        });
    };
    return {
        //returns true if there is an auth token
        isAuthenticated: function() {
            var storedJwt = $window.sessionStorage.authToken;
            console.log('stored JWT: '+storedJwt);
            var storedPayload = jwtHelper.decodeToken(storedJwt);
            console.log('payload: '+JSON.stringify(storedPayload));
            if(jwtHelper.isTokenExpired(storedJwt)){
                console.log('is expired expired: '+jwtHelper.getTokenExpirationDate(storedJwt));
                _this.delegate();
            } else {
                console.log('is not expired expires: '+jwtHelper.getTokenExpirationDate(storedJwt));
                //For testing
                _this.delegate();
            }
            return $window.sessionStorage.authToken;
            //LocalService.get('authToken');
        },
        delegate: delegate,
        //login function, should be moved to login controller
        login: function(email, password) {
            var login = $http.post(API_URL+'/authenticate', {email: email, password: password } );
            login.success(function(result) {
                console.log('login-result: '+JSON.stringify(result));
                $window.sessionStorage.authToken = result.token;
                $window.sessionStorage.refreshToken = result.refresh_token;
                $location.path('/about');
                //LocalService.set('authToken', JSON.stringify(result));
            });
            return login;
        },