Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过本地存储获取AngularJS中的特定状态?_Angularjs_Html - Fatal编程技术网

如何通过本地存储获取AngularJS中的特定状态?

如何通过本地存储获取AngularJS中的特定状态?,angularjs,html,Angularjs,Html,我想在页面刷新后通过应用程序中的本地存储更改状态。如何获得国家 HTML: <body ng-app="sampleApp"> <div class="container" ng-controller="Registration" ng-init ="temp()"> <div ui-view></div> </div> var sampleApp = angular.module('sampleApp',

我想在页面刷新后通过应用程序中的本地存储更改状态。如何获得国家

HTML:

<body ng-app="sampleApp"> 
   <div class="container" ng-controller="Registration" ng-init ="temp()">
     <div ui-view></div>
 </div>
var sampleApp = angular.module('sampleApp', 
                        ['ui.router','angularFileUpload','ngStorage']);

        sampleApp.config(function($stateProvider, $urlRouterProvider) { 
        $stateProvider  
            .state('settings', {
                url: '/settings',
                templateUrl: '/ui/step1.htm'
           .state('settings/Personal_Info', {
            url: '/Personal_Info',
            templateUrl: '/ui/step2.htm',
            //controller: 'ProfileController'
        })
        .state('settings/Personal_Info_1', {
            url: '/Personal_Info_1',
            templateUrl: '/ui/step3.htm',
            //controller: 'AccountController'
        })
        .state('settings/P_Info_Affiliations', {
            url: '/P_Info_Affiliations',
            templateUrl: '/ui/step4.htm',
            //controller: 'ProfileController'
        });
            $urlRouterProvider.otherwise('/settings');
        });
    sampleApp.controller("Registration", 
         function ($scope, $location, $http, $state, $localStorage){
            $scope.currentstep = 1;
            $scope.formData = {};

            $scope.temp = function()
            { alert($localStorage.currentstep);
                if($localStorage.currentstep > 0) {
                    $scope.currentstep = $localStorage.currentstep; 
                    $scope.getStepUrl();
                }
            }
         $scope.next = function(formData){ 
    console.log($scope.currentstep);
    //console.log($scope.formData);
        if (formData.check == 1) {
            $scope.currentstep = $scope.currentstep + 1;
            $localStorage.currentstep = $scope.currentstep;
            alert("Everything is validated. We can go ahead");

            $http({
                method  : 'POST',
                url     : 'addstep?step'+ $scope.currentstep,
                data    : $scope.formData, 
                headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
            })
            .success(function(data) {
                if (data.errors) {
                    $scope.errorName = data.errors.name;
                    $scope.errorUserName = data.errors.username;
                    $scope.errorEmail = data.errors.email;
                } else {
                    $scope.message = data.message;
                }
            });
            $scope.getStepUrl();
        }
        else {
            alert("Some issues in input data.");
        }  
    }
        $scope.getStepUrl = function() {

           if($scope.currentstep == 1){
               $state.go('settings');
            }
            else if($scope.currentstep == 2){
                $state.go('settings/Personal_Info');
                alert("i am step 2");
            }
            else if($scope.currentstep == 3){
                $state.go('settings/Personal_Info_1');
            }
         }

 });    
我无法正确获取状态。
如何解决这个问题?
提前感谢您的帮助。

删除默认状态

$urlRouterProvider.otherwise('/settings');
在函数中使用else语句

if($localStorage.currentstep > 0) {
            $scope.currentstep = $localStorage.currentstep;

            $scope.getStepUrl();
        }else{
            $state.go('settings');
        }

您可以通过$state提供程序和
state.current.name

因此,您可以询问要使用的每个控制器或服务的当前状态

,但您在哪里设置“$localStorage.currentstep”?我认为可以在getStepUrl方法中设置适当的'step'值