簡體   English   中英

如何在angularjs中使用會話

[英]how to use session in angularjs

我正在創建一個Web應用程序,我想在其中通過Angularjssession存儲用戶ID和角色

這是我的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中存儲和使用會話?

 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');
}]);

旁觀

您可以為此使用會話存儲或本地存儲,這是會話存儲的示例。

會話存儲或本地存儲在堆棧溢出時將無法正常工作,因此請參考給定的鏈接。

鏈接

 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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM