本文介绍了保留页面刷新后角变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找出一种方法来保持我的角
与页面刷新/变量跨控制器。我的工作流程是,
I'm trying to figure out a way to keep my angular
variables with page refresh / across controllers. My workflow is,
- 的用户通过Facebook登录并获得访问令牌
- 用户访问令牌将每个请求使用
我试了两种方式,
1 - 分配令牌到 rootScope
不工作
1 - Assigning the token to a rootScope
Not working
2 - 通过使用工厂
2 - By using a factory
#app.js
'use strict';
angular.module('recipeapp', [])
.run(['$rootScope', '$injector', 'Authenticate', function($rootScope,$injector, Authenticate){
$injector.get("$http").defaults.transformRequest = function(data, headersGetter) {
$injector.get('$http').defaults.headers.common['auth-token'] = Authenticate.getToken();
}
}]);
#factory
'use strict';
angular.module('recipeapp')
.factory('Authenticate', function(){
var factory = {};
var accessToken = "";
factory.setToken = function(token) {
accessToken = token;
}
factory.getToken = function() {
return accessToken;
}
return factory;
})
#facebook controller
I set the the token with every successful login
Authenticate.setToken(data.app_token);
但问题是,如果我刷新页面, Authenticate.getToken()
变为空白。我是pretty新角
并不能想出一个办法页面刷新后保留我的数据
But the problem is, If I refresh the page, Authenticate.getToken()
becomes blank. I'm pretty new to angular
and cannot figure out a way to retain my data after a page refresh
任何帮助将非常AP preciated
any help would be much appreciated
推荐答案
您可以使用的localStorage
。这是很容易使用。
You can use localStorage
. It is really easy to use.
var token = "xxx";
localStorage.setItem("token", token);
localStorage.getItem("token"); //returns "xxx"
这篇关于保留页面刷新后角变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!