简体   繁体   English

在IONIC中检查state.go的本地存储值并传递参数

[英]Check localstorage value and pass parameters at state.go in IONIC

I have create an ionic app that will set the token value whenever a successful login is made by the user. 我创建了一个ionic应用程序,该应用程序将在用户成功登录后设置令牌值。 Then, it will make use of $state.go to redirect the user to another tab "tab.scan". 然后,它将利用$ state.go将用户重定向到另一个选项卡“ tab.scan”。

When user clicked on the "tab.details" tab at the navigation bar, it will redirect the user to "tab.details" without passing any parameters. 当用户单击导航栏上的“ tab.details”选项卡时,它将在不传递任何参数的情况下将用户重定向到“ tab.details”。

However, the "details" page did not manage to get the value of the stored token. 但是,“详细信息”页面无法获取已存储令牌的值。 How can I go about doing it? 我该怎么做呢?

It works fine when the url is url: '/details' instead of url: '/details/:store_id/:invoice_id' . 当url为url: '/details'而不是url: '/details/:store_id/:invoice_id'时,它可以正常工作。

Code for login 登录代码

.controller('LoginCtrl', function ($scope, LoginService, $ionicPopup, $state, $stateParams) {
    $scope.data = {};
    $scope.token = { token: "" };

    $scope.login = function () {
        LoginService.loginUser($scope.data.username, $scope.data.password).success(function (data) {
            //Set login token
            localStorage.setItem("token", $scope.data.username);
            //Redirect the user to scan tab
            $state.go('tab.scan', { store_id: $scope.data.store_id });
            //Popup alert for welcome message
            var alertPopup = $ionicPopup.alert({
                title: 'Login Success!',
                template: 'Welcome ' + localStorage.getItem("token") + '!'
            });
        }).error(function (data) {
            //Failed login, popup error message
            var alertPopup = $ionicPopup.alert({
                title: 'Login Failed!',
                template: 'Please check your credentials!'
            });
        });
    }
})

Code for tabs 标签代码

.state('tab.details', {
      cache: false,
      url: '/details/:store_id/:invoice_id',
      views: {
        'tab-details': {
          templateUrl: 'templates/tab-details.php',
          controller: 'DetailsCtrl'
        }
  }
})

Code for detail controller 详细控制器代码

.controller('DetailsController', function ($scope, $http, $state, $ionicPopup, $stateParams) {
    $scope.token = localStorage.getItem("token");
    console.log($scope.token);
    if ($scope.token == "logOut") {
        $state.go('login');
        var alertPopup = $ionicPopup.alert({
            title: 'Login Required!',
            template: 'Please login to access.'
        });
    }
    else {
    }
})

$scope is relative to each controller unless login is a parent state $scope will not cascade to child states. $ scope是相对于每个控制器的,除非登录名是父状态,否则$ scope不会级联到子状态。 You need to set token in a service which can be called from any controller or you can set it on $rootScope so it is available everywhere. 您需要在可以从任何控制器调用的服务中设置令牌,也可以在$ rootScope上设置令牌,以便可以在任何地方使用它。 Is you want to use $rootScope change $scope.token to $rootScope.token. 您是否要使用$ rootScope将$ scope.token更改为$ rootScope.token。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM