我正在创建一个网络应用程序,我想通过 Angularjs
在 session
中存储用户 ID 和 Angular 色
这是我的 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";
}
}
})
我需要在我的 session 中存储 $scope.pm
和 $scope.log
并想在我的欢迎页面上使用它们
如何在angularjs中存储和使用session?
最佳答案
您可以为此使用 session 存储或本地存储,这里是 session 存储的示例。
session storage or local storage will not work here in stack overflow so refer given link.
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>
关于javascript - 如何在angularjs中使用session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40500867/