angularjs中sessionstorage的常用服务

时间:2016-02-16 04:12:35

标签: angularjs

您好我的应用程序我在登录控制器中设置值并获取所有其他js文件,除此之外如何使用公共服务设置存储并在必需的js文件中获取该存储  我的登录控制器

app.controller('LoginController',function(loginService, $rootScope,$scope, $http,$location) {

     $scope.login = function () {
        $scope.log=loginService.getLogin( $scope.emailId , $scope.password).
         then(function (response) {
            console.log($scope.log);
            console.log(response)
                     if (response.data.LoginVerificationResult.length === 0) {
                        alert('details are not Available for this emailId');
                        $scope.error=true;

                     } else {
                         $rootScope.name=response.data.LoginVerificationResult[0].UserName;
                         $scope.abc=response.data.LoginVerificationResult[0].UserType
                 console.log($scope.abc+"from.......");
                         sessionStorage.setItem("EmaiId",$scope.emailId);
                         sessionStorage.setItem("User Id",response.data.LoginVerificationResult[0].UserID);
                         sessionStorage.setItem("UserName",response.data.LoginVerificationResult[0].UserName);
                         sessionStorage.setItem("UserType",response.data.LoginVerificationResult[0].UserType);

                            $scope.UserType = sessionStorage.getItem("UserType");
                            console.log($scope.UserType +"from login controller")

                                 $location.path('/dashboard')


                     }

         });
     };
});

我的changepassword文件

app.controller("ChangePwdController", function($scope, $http, $location,
        BaseUrl, changePwdService) {
    //$scope.roleId = sessionStorage.getItem("Role ID");
/*  $scope.UserType = sessionStorage.getItem("UserType");*/
    $scope.username = sessionStorage.getItem("UserName");
    $scope.userType = sessionStorage.getItem("UserType");
    $scope.EmpName=sessionStorage.getItem("EmpName");
    $scope.patientName=sessionStorage.getItem("PatientName")
    $scope.changePwd = function() {
        $scope.emailAddress = sessionStorage.getItem("EmaiId");
        console.log($scope.emailAddress)
        var data = {

            'emailAddress' : $scope.emailAddress,
            'currentPassword' : $scope.opassword,
            'newPassword' : $scope.npassword

        };
        console.log("Hi")
        $scope.pwd=changePwdService.postChangePwd(data).success(
                function(resp) {
                    $scope.PostDataResponse = data;
                    console.log($scope.pwd)
                    console.log($scope.PostDataResponse);
                    if (resp.ResetPasswordResult === true) {
                        alert("Successfully changed");
                        console.log("success")
                        $location.path('/dashboard');

                    } else {
                        console.log("fail")
                        alert("Enter valid current password")
                    }

                })
    }
})

是否有其他方法可以设置和获取一个文件

1 个答案:

答案 0 :(得分:0)

有些方法可以达到同样的目的。请在此处参考this