Javascript Can';t更新Angularjs工厂内的变量

Javascript Can';t更新Angularjs工厂内的变量,javascript,angularjs,angularjs-factory,Javascript,Angularjs,Angularjs Factory,有人能帮忙吗?无法更新varABCKey。执行setAuthenticatedAccount并返回正确的值。之后,运行getAuthenticatedAccount,并接收undefined angular.module('authentication.service', [ ]) .factory('Authentication', ['$state', '$cookies', '$http', 'app', 'routes', 'cfCryptoHttpInterceptor',

有人能帮忙吗?无法更新varABCKey。执行setAuthenticatedAccount并返回正确的值。之后,运行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;
}]);

不要使用它的单音类,你需要在这里定义ABCkey

var ABCKey

试试这个

this.ABCKey = '';

不要使用它的单音类,你需要在这里定义ABCkey

var ABCKey

试试这个

this.ABCKey = '';

在调用函数时删除身份验证,因为它每次都在创建对象。并在减速时设置
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;
}]);

在调用函数时删除身份验证,因为它每次都在创建对象。并在减速时设置
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;
}]);

在服务中我们可以使用它,而不是在工厂中。是的。这应该在服务中使用,但我们也可以在工厂中使用,检查:在服务中我们可以使用它,而不是在工厂中。是的。这应该在服务中使用,但我们也可以在工厂中使用,检查:这样,console.log返回“null”,console.log返回“null”
getAuthenticatedAccount
函数在后需要一个。
getAuthenticatedAccount
函数在后需要一个。