angularjs - localstorage具有值时的实时显示选项卡

时间:2017-05-09 12:46:14

标签: javascript angularjs ionic-framework

我正在尝试隐藏某些标签,并仅在用户名localstorage变量用户名包含值时启用它。我能够正确隐藏但是当localstorage获取值时,选项卡不会显示

<ion-tabs  ng-controller="tabs_ctrl" class="tabs-icon-top  tabs-positive">


  <ion-tab ng-if ="username==null" title="Sign In" icon="ion-log-in" href="#/tab/signin">
    <ion-nav-view name="tab-signin"></ion-nav-view>
  </ion-tab>


  <ion-tab ng-if ="username==null" title="Create Account" icon="ion-person">
    <ion-nav-view name="tab-create_account"></ion-nav-view>
  </ion-tab>



  <ion-tab ng-if ="username!=null" title="My Account" icon="ion-person" href="#/tab/account">
    <ion-nav-view name="tab-user_account"></ion-nav-view>
  </ion-tab>


  <ion-tab ng-if ="username!=null" title="Billing" icon="ion-ios-paper" href="#/tab/billing">
    <ion-nav-view name="tab-billing"></ion-nav-view>
  </ion-tab>
</ion-tabs>

标签控制器

.controller('tabs_ctrl',['$scope',function($scope){
    $scope.username = localStorage.getItem("username");
             }])

这是我的登录控制器,当用户登录正确时,我设置了用户名localstorage值,但视图没有更新,以显示用户登录时的用户选项卡

.controller('login_ctrl',['$scope','$http','$timeout','$ionicModal','$ionicLoading','$ionicPopup','$location','$cordovaToast','$location',function($scope,$http,$timeout,$ionicModal,$ionicLoading,$ionicPopup,$location,$cordovaToast,$location){

    $scope.mainlogin= function(){
    $ionicLoading.show({template: '<p>Please Wait...</p><ion-spinner></ion-spinner>'});
        event.preventDefault();
        $http.post("http://localhost/myapp/scripts/login.php",
        {'username':$scope.username,'password':$scope.password})
        .success(function(data){
        console.log(JSON.stringify(data));

            {$ionicLoading.hide();}

            if(data==="Incorrect Username or Password"){
            $scope.mainloginAlert()
    }
                else{
                if(data != "Incorrect Username or Password"){
                localStorage.setItem("customer_name",(data[0].customer_name));
                localStorage.setItem("account_number",(data[0].account_number));
                localStorage.setItem("username", $scope.username)
                $location.path = '#/tab/create_account';
                    }
                }

                }).error(function(error){
                console.error(error);
                {$ionicLoading.hide();}
                $scope.connect()
                    });

  }


}])

1 个答案:

答案 0 :(得分:0)

因为localStorage.getItem(&#34; username&#34;)正在返回一个承诺。

您必须使用then来获取getItem()值。

像这样:

localStorage.getItem(&#34; username&#34;)。then(function(username){$ scope.username = username})