gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:24:46 24 4
gpt4 key购买 nike

有人可以帮忙解决这个问题吗?无法更新变量 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com