Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.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对象异常_Javascript_Angularjs - Fatal编程技术网

地址栏角度应用程序刷新/深度链接中的Javascript对象异常

地址栏角度应用程序刷新/深度链接中的Javascript对象异常,javascript,angularjs,Javascript,Angularjs,我使用Angular 1.3.15和UI路由器,托管在IIS 7.5上 以下代码包含$stateChangeStart事件的设置。当应用程序已从主链接加载时,此代码被正确调用;当用户使用角色访问状态时,没有问题。当您试图通过地址栏手动点击链接或刷新当前页面并重新加载应用程序时,函数将运行,但身份验证.profile对象上的任何属性都为空。当$stateChangeStart第一次触发时,您将看到我正在执行一个console.dir(authentication.profile)。这表明确实存在数

我使用Angular 1.3.15和UI路由器,托管在IIS 7.5上

以下代码包含
$stateChangeStart
事件的设置。当应用程序已从主链接加载时,此代码被正确调用;当用户使用角色访问状态时,没有问题。当您试图通过地址栏手动点击链接或刷新当前页面并重新加载应用程序时,函数将运行,但
身份验证.profile
对象上的任何属性都为空。当
$stateChangeStart
第一次触发时,您将看到我正在执行一个
console.dir(authentication.profile)
。这表明确实存在数据和对象上的方法。但是如果我尝试
console.dir(authentication.profile.token)
,它是空的

我不确定这是否与从不同路径刷新应用程序或完全不同的内容有关

任何帮助都将不胜感激

'use strict';

var serviceId = 'authentication';
angular.module('app').factory(serviceId, 
       ['common', '$localForage', '$injector', authentication]);

angular.module('app').run(['$rootScope','$state', '$stateParams', 
  'authentication', function ($rootScope,$state, $stateParams, authentication) {
    $rootScope.$on('$stateChangeStart', 
        function (e, toState, toParams, fromState, fromParams) {
        console.dir(authentication.profile);
        if (toState.data.roles.length > 0) {
            console.log('has roles');
            console.dir(authentication.profile.roles());
            if (!authentication.profile.isInAnyRole(toState.data.roles)) {
                e.preventDefault();
                $state.go('home');
            }
        }
    });
}]);
身份验证服务:

function authentication(common, $localForage, $injector) {
    var USERKEY = "utoken";

    var setProfile = function (username, token) {
        profile.username = username;
        profile.token = token;
        localforage.setItem(USERKEY, { 
             'username': username, 
             'token': token 
        })
    };

    var initialize = function () {
        var user = {
            username: "",
            token: "",

            isAuthenticated: function () {
                return this.token;
            },
            isUserInRole : function (role) {
                if (this.token != "") {
                    var decoded = jwt_decode(this.token);
                    return decoded.role.indexOf(role) != -1;
                }
                else return false;

            },
            isInAnyRole: function (roles) {

                for (var i = 0; i < roles.length; i++) {
                    if (this.isUserInRole(roles[i])) return true;
                }

                return false;
            },
            roles: function(){
                if (this.token != "") {
                    var decoded = jwt_decode(this.token);
                    return decoded.role;
                }
                else return false;
            },
            isTokenExpired: function () {
                var decoded = jwt_decode(this.token);
                if (moment.unix(decoded.exp).isBefore( moment())) {
                    return true;
                }
                else
                    return false;
            }
        };



        var localUser = null;
        $localForage.getItem(USERKEY).then(function (data) {
            localUser = data;
            if (localUser) {
                user.username = localUser.username;
                user.token = localUser.token;
                if (user.isTokenExpired())
                    logout();
            }
        });


        return user;
    };

    var logout = function () {
        profile.username = "";
        profile.token = "";
        $localForage.removeItem(USERKEY);
        $injector.get('$state').transitionTo('home');
    };

    var profile = initialize();

    return {
        setProfile: setProfile,
        profile: profile,
        logout: logout
    };
};
功能验证(公共、$localfour、$injector){
var USERKEY=“utoken”;
var setProfile=函数(用户名、令牌){
profile.username=用户名;
profile.token=令牌;
setItem(USERKEY,{
“用户名”:用户名,
“令牌”:令牌
})
};
变量初始化=函数(){
变量用户={
用户名:“”,
令牌:“”,
isAuthenticated:函数(){
返回此.token;
},
isUserInRole:函数(角色){
如果(this.token!=“”){
var decoded=jwt_decode(this.token);
返回已解码的.role.indexOf(role)!=-1;
}
否则返回false;
},
角色:功能(角色){
对于(变量i=0;i
您希望出现哪种行为?当您重新加载页面或“手动”导航到URL时,应用程序将从头开始。而且您从不调用
authentication.setProfile()

要保留会话,在登录、重新加载或手动导航时,您必须保留这些数据以供脱机使用。您可以使用cookies或


然后,在状态跟踪代码中,一旦检测到丢失的配置文件并尝试从存储中还原它,您将读取存储/cookie。从处理oauth端点的另一个服务调用setProfile()。正如我在前面的评论中所说,如果您在authentication.profile对象激发后立即通过console.dir查看它,您可以在那里看到数据。但是如果我在代码中尝试它的任何属性或方法,我都不会得到预期的数据该服务的代码在哪里?调用
authentication.setProfile()
时,从何处读取用于身份验证的数据`