问题描述
执行该功能后,在浏览器中创建会话。然后关闭并打开会话将清除的同一浏览器。
after execute the function the session created in browser. and i close and open the same browser the session will cleared.
$scope.logIn = function () {
$http(
{
method: 'post',
url: 'Service.asmx/fn_CheckLogin',
data: $.param({ strMob: $scope.login.mob, strPwd: $scope.login.pwd }),
dataType: 'json',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function (data, status, headers, config) {
sessionStorage.user = data[0].UserName;
sessionStorage.loggedIn = true;
}).error(function (data, status, headers, config) {
sessionStorage.user = '';
sessionStorage.loggedIn = false;
});
};
推荐答案
回答OP添加的评论。
是的,可以在角度控制器中的文档就绪上执行代码。
Yes, it is possible to execute code on document ready in your angular controller.
在您的基础中附加以下事件(main )controller:
Append following event in you base( main) controller:
angular.element(document).ready(function () {
// code you need to execute on document ready
});
会话存储:
编辑(回答Op对此答案的评论):
Edit (answer to Op's comment on this answer):
要在用户关闭浏览器后保持信息,您需要用户 localStorage
。
To maintain information even after user closes browser, You need to user localStorage
.
Google it,它与sessionStorage的使用类似,易于理解。找到这个来帮助你。仍然阅读它,然后才使用它。
Google it, it is similar to sessionStorage in usage and is easy to understand. Found this codepen to help you maybe. Still read about it and only then use it.
此外,如果你开放讨论,我建议你只在 localStorage存储用户名
而不是像logedIn这样的标志,而是在服务器上维护这些信息。然后,每当用户关闭一个系统上的浏览器并从其他系统登录并注销时。他将从他使用的所有系统中退出。这是大多数Web应用程序的可接受行为。
Also if you are open for discussion, I will suggest you to store just the username in localStorage
and not a flag like logedIn instead maintain this information on server . Then whenever user closes browser on one system and logs in from other system and logs out. He will be logged out from all systems he has used. This is accepted behavior for most of the web applications.
这篇关于重新打开浏览器后丢失会话存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!