javascript - 无法更新 Angularjs Factory 内的变量

标签 javascript angularjs angularjs-factory

有人可以帮忙解决这个问题吗?无法更新变量 ABCKey。执行 setAuthenticatedAccount 和 console.log 返回正确的值。之后,运行 getAuthenticatedAccount,并收到 undefined。

angular.module('authentication.service', [
])

.factory('Authentication', ['$state', '$cookies', '$http', 'app', 'routes', 'cfCryptoHttpInterceptor',
    function($state, $cookies, $http, app, routes, cfCryptoHttpInterceptor) {

    var ABCKey;

    var setAuthenticatedAccount = function (account, tokenAuth) {
        var accountInfo = {'email': account.email, 'username': account.username, 'token': tokenAuth}
        var abc = CryptoJS.enc.Base64.parse(account.abc);

        Authentication.setABCKey(abc);

        console.log(Authentication.showABCKey())

    }

    var getAuthenticatedAccount = function() {
        if(!$cookies.authenticatedAccount) {
            return;
        }

        console.log(Authentication.showABCKey())

    }


    var setABCKey = function(key) {
        ABCKey = key;
    };

    var showABCKey = function() {
        return ABCKey;
    };

    var Authentication = {
        setAuthenticatedAccount: setAuthenticatedAccount,
        getAuthenticatedAccount: getAuthenticatedAccount,
        setABCKey: setABCKey,
        showABCKey: showABCKey 
    };

    return Authentication;
}]);

最佳答案

在调用函数时删除身份验证,因为它每次都会创建对象。并且在减速时也设置 var ABCKey=null 像这样 -

angular.module('authentication.service', [
])

.factory('Authentication', ['$state', '$cookies', '$http', 'app', 'routes', 'cfCryptoHttpInterceptor',
    function($state, $cookies, $http, app, routes, cfCryptoHttpInterceptor) {

    var ABCKey=null;

    var setAuthenticatedAccount = function (account, tokenAuth) {
        var accountInfo = {'email': account.email, 'username': account.username, 'token': tokenAuth}
        var abc = CryptoJS.enc.Base64.parse(account.abc);

        setABCKey(abc);

        console.log(showABCKey())

    }

    var getAuthenticatedAccount = function() {
        if(!$cookies.authenticatedAccount) {
            return;
        }

        console.log(showABCKey())

    }


    var setABCKey = function(key) {
        ABCKey = key;
    };

    var showABCKey = function() {
        return ABCKey;
    };

    var Authentication = {
        setAuthenticatedAccount: setAuthenticatedAccount,
        getAuthenticatedAccount: getAuthenticatedAccount,
        setABCKey: setABCKey,
        showABCKey: showABCKey 
    };

    return Authentication;
}]);

关于javascript - 无法更新 Angularjs Factory 内的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44908325/

相关文章:

javascript - Ajax post请求漏洞

javascript - Node.js 异步函数

javascript - AngularJS:将 Angular 服务属性绑定(bind)到范围

java - AngularJS中的Spring Security返回403禁止登录

javascript - AngularJS : factory to return data from a php service using $http

javascript - 如何在服务器端 Blazor 中使用 Bing Javascript map ?

javascript - 如何使用 javascript 以编程方式编辑 HTML 格式的文本,而无需手动处理标签和 HTML

javascript - 无法加载 base64 图像

javascript - 未调用 AngularJS 服务

javascript - Angular 状态的问题及解决方案