的解决方案取决于如下所示的SessionService
类。语法是coffeescript。
SessionService类
class SessionService
scopes:[]
setStorage:(key, value) ->
scope[key] = value for scope in @scopes
value = if value is undefined then null else JSON.stringify value
sessionStorage.setItem key, value
getStorage:(key)->
sessionValue = sessionStorage.getItem key
if sessionValue == "undefined"
return null
JSON.parse sessionValue
register:(scope)->
for key, value of sessionStorage
scope[key] = if value? and value != "undefined" then JSON.parse(value) else null
@scopes.push scope
scope.$on '$destroy', =>
@scopes = @scopes.filter (s) -> s.$id != scope.$id
clear: ->
@setStorage(key, null) for key of sessionStorage
isAuthenticated: ->
@accessor 'isAuthenticated', value
user:(value=null) ->
@accessor 'user', value
# other storage items go here
accessor:(name, value)->
return @getStorage name unless value?
@setStorage name, value
angular
.module 'app.Services'
.service 'sessionService', SessionService
的SessionService
类定义了isAuthenticated
属性(简单布尔)和user
属性(一个复杂的对象)。这些属性的值是自动字符串化/解析为它们被存储/使用客户端侧由JavaScript提供本地sessionStorage
对象检索。
你加更多的属性需要。和$rootScope
一样,你要谨慎地添加属性。不像$rootScope
属性值仍刷新页面或后退按钮点击后可用。
该服务允许用它来注册的任何数量的作用域。当一个范围被登记在sessionStorage
所有存储的值被自动分配给该范围。通过这种方式,所有注册的范围始终可以访问所有会话属性。
当属性值被更新,所有注册的范围有其相应的值更新。
当角销毁一个范围内,可以自动地从注册范围的列表中删除以节省浪费资源。
如果用户刷新页面或者点击后退按钮,然后角应用被强制重新启动。通常这意味着你将不得不重建你的当前状态。该SessionService
自动为您完成此,因为每个范围将是从本地存储恢复它的价值时,他们的应用程序初始化期间注册。
所以,现在很容易解决范围之间的两个数据共享的问题,以及当用户刷新或点击后退按钮恢复值。
下面是一些示例角度代码,显示如何使用SessionService
类。
在某些控制器注册与SessionService一个范围
angular
.module 'app'
.controller 'mainCtrl', ($scope, $state, session, security) ->
#register the scope with the session service
session.register $scope
#hook up the 'login' method (see security service)
$scope.login = security.login
# check the value of a session property
# it may well be true if the page has been refreshed
if session.isAuthenticated
$state.go('home')
else
$state.go('login')
在服务
class SecurityService
@$inject:['$http','sessionService', 'api']
constructor:(@http, @session, @api) ->
login:(username, password) =>
@http.get "#{@api.base}/security/login/credentials/#{username}/#{password}"
.success (user)=>
@session.isAuthenticated = true
@session.user = user
.error (ex)=>
# process error
angular
.module 'app'
.service 'securityService', SecurityService
在UI(玉模板)使用Session值
设置会话值
div(ng-show="isAuthenticated")
div Hello {{user.Name}}
有没有这个非咖啡标签版本?这也是唯一可行的解决方案吗?谢谢! – EKet
您可以将coffeescript转换为javascript,然后再回到这里:http://js2coffee.org/。一定会有其他更好的解决方案,起初我认为必须有一种简单,有角度的方式来处理诸如后退/刷新按钮之类的常见事情。但是我找不到它,所以你在这里看到的是我最好的,最好的,使它的工作。我希望有人发布更好的解决方案。 – biofractal
+1为'为什么人们发布咖啡角标解决方案?' – edzillion