如何在angularjs中使用session
how to use session in angularjs
我正在创建一个web应用程序,我想在其中通过Angularjs
将用户ID和角色存储在session
中
这是我的Angularjs文件
$http.get('/login.asmx/loginuser', {
params: {
log: $scope.log,
pm: $scope.pm,
password: $scope.password
}
})
.then(function (response) {
{
$scope.suc = response.data;
console.log(response.data);
if (response.data == 'success') {
console.log('success');
$window.location.href = "../welcomepage/welcometable";
}
else
{
console.log('nosuccess');
$window.location.href = "../Login/Index";
}
}
})
我需要在会话中存储$scope.pm
和$scope.log
,并希望在我的欢迎页面上使用它们
如何在angularjs中存储和使用会话?
您可以使用会话存储或本地存储,以下是会话存储的示例。
在堆栈溢出的情况下,会话存储或本地存储将不起作用所以参考给定的链接。
链接
window.addCart = function($scope, $http, $window, $document){
var getValue = function(){
return $window.sessionStorage.length;
}
var getData = function(){
var json = [];
$.each($window.sessionStorage, function(i, v){
json.push(angular.fromJson(v));
});
return json;
}
$scope.images = getData();
$scope.count = getValue();
$scope.addItem = function(id){
var image = document.getElementById('img'+id);
json = {
id: id,
img: image.src
}
$window.sessionStorage.setItem(id, JSON.stringify(json));
$scope.count = getValue();
$scope.images = getData();
}
$scope.removeItem = function(id){
$window.sessionStorage.removeItem(id);
$document.
$scope.count = getValue();
$scope.images = getData();
alert('Removed with Success!');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>
<div ng-app="">
<div ng-controller="addCart">
<p>{{ count }}</p>
<div>
<img id="img16" src="http://placehold.it/351x350"/>
<a href="javascript:void(0)" ng-click="addItem('16')">Add to Cart</a>
</div>
<div>
<img id="img5" src="http://placehold.it/352x350"/>
<a href="javascript:void(0)" ng-click="addItem('5')">Add to Cart</a>
</div>
<div>
<img id="img32" src="http://placehold.it/353x350"/>
<a href="javascript:void(0)" ng-click="addItem('32')">Add to Cart</a>
</div>
<div>
<img id="img43" src="http://placehold.it/354x350"/>
<a href="javascript:void(0)" ng-click="addItem('43')">Add to Cart</a>
</div>
<hr />
<table>
<thead>
<td>Image</td>
<td>Options</td>
</thead>
<tbody>
<tr ng-repeat="data in images" id>
<td><img ng-src="{{ data.img }}"/></td>
<td><a href="javascript:void(0)" ng-click="removeItem(data.id)">Remove Item {{ data.id }}</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
angular.module('cookieStoreExample', ['ngCookies'])
.controller('ExampleController', ['$cookieStore', function($cookieStore) {
// Put cookie
$cookieStore.put('myFavorite','oatmeal');
// Get cookie
var favoriteCookie = $cookieStore.get('myFavorite');
// Removing a cookie
$cookieStore.remove('myFavorite');
}]);
查看
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- AngularJS:我可以跳过函数参数回调吗
- AngularJS UI路由器不能像ng路由器那样工作
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- 如何包含特定于每个视图angularjs的javascript文件
- AngularJS:ng之后,重复$scope值未按预期更新
- AngularJS-在JSON选择器中使用变量名
- $window.ga在AngularJS事件中未定义
- AngularJS-使用'true'属性
- 打开一个模态并将其链接到AngularJS中的指令
- Angularjs代码未在匿名函数中运行
- angularjs+rails应用程序中未显示模板
- 我的AngularJS表达式没有'不起作用
- AngularJS JSON not arriving php
- 使用AngularJS中的筛选器更新给定的表
- Angularjs工厂注入错误
- AngularJS设计指南
- Angularjs-utils高亮过滤器在搜索时破坏应用程序
- 使用angularjs验证文本框中的电子邮件
- 如何在angularjs中使用session