Ionic 侧菜单:刷新 rootPage

阿方索·西尔维斯特里

我在使用 Ionic 的侧菜单模板时遇到问题。我有一个这样的菜单:在此处输入图片说明

其中“登录/注销”与页面 login.html 匹配,就像根页面一样。你可以在名为 app.component.ts 的 ionic 文件中查看:

import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';

import { Login } from '../pages/login/login';
import { Page2 } from '../pages/page2/page2';
import { Page3 } from '../pages/page3/page3';


@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;

  rootPage: any = Login;

  pages: Array<{title: string, component: any}>;

  constructor(public platform: Platform) {
    this.initializeApp();

    // used for an example of ngFor and navigation
    this.pages = [
      { title: 'Login/Logout', component: Login },
      { title: 'Page Two', component: Page2 },
      { title: 'Page Three', component: Page3 }
    ];

  }

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      StatusBar.styleDefault();
      Splashscreen.hide();
    });
  }

  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.nav.setRoot(page.component);
  }
}

和 app.module.ts:

import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { Login } from '../pages/login/login';
import { Page2 } from '../pages/page2/page2';
import { Page3 } from '../pages/page3/page3';

@NgModule({
  declarations: [
    MyApp,
    Login,
    Page2,
    Page3
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    Login,
    Page2,
    Page3
  ],
  providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}]
})
export class AppModule {}

这是login.html的代码:

<ion-header>
  <ion-navbar>
    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>Login</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <h3>Effettua l'accesso</h3>

  <p>
    Esegui il login oppure procedi come utente non registrato. Clicca in alto a sinistra per vedere il menu.
  </p>
  <form  ng-show="credentials" ng-submit="submitCredentials()">
    <div>
        <div>
            <div>Email</div> 
            <div>
            <input type="text" id="email" ng-model="userLoginForm.email" />
            </div>
        </div>
        <div>
      <div>Password</div>
      <div>
        <input type="password" id="password" ng-model="userLoginForm.pass" />
      </div>
     </div>
    <div>
      <div colspan="2" class='centered'>
        <button ion-button type="submit">Login</button>
      </div>
    </div>
    </div>
</form>
<form ng-show="logout" ng-submit="userLogout()">
    <div>
      <div colspan="2" class='centered'>
        <button ion-button type="submit">Logout</button>
      </div>
    </div>
</form>
    
        
</ion-content>

这是 logincontroller.js:

'use strict'; //questo mi da informazione sull'errore

var app = angular.module("UserManagement", ['ngRoute']);  

app.controller("LoginController", function($scope, $http, $window, restService) {  
    
    $scope.credentials = false;
    $scope.logout = false;
    
    var token = localStorage.getItem("token");
    if (token !== null){
     //   $window.location.href="login.html";
        $scope.credentials = false;
        $scope.logout = true;
        console.log("loggato");
    }
    else{
      //  $window.location.href="page2.html";
        $scope.credentials = true;
        $scope.logout = false;
        console.log("non loggato");   
    }
    $scope.userlogins = [];
    $scope.userLoginForm = {  
        email: "",
        pass: ""
    };
    $scope.tokenUserForm = {  
        idtokenuser: -1,    
        tokenuser: ""
    };  
    $scope.userForm = {  
        iduser : -1,    
        name: "",
        surname: "",
        birthDate: "",
        cityResidence: "",
        provinceResidence: "",
        postalCode: "",
        gender: true,
        userLogin: {
            iduserLogin: -1,
            email: "",
            pass: "" 
          }
    };  
    
   
    
    //8njae3j4b54fpoapftc8aofbfs
    
    //admin: l5qsngh3v9a5f2v9p55ar4h083
    $scope.submitCredentials= function() { 
        console.log("LOGINFORM: "+$scope.userLoginForm.pass);
        restService.login($scope.userLoginForm, _logsuccess, _logerror);
        
    $scope.userLogout= function() { 
        restService.logout;
    }
    
    function _logsuccess(response) {  
        console.log("Loggato correttamente");
        console.log(response.status);
        var CurrentToken = response.data;
      //  _SetToken(CurrentToken);                                        
    }  
            
    function _logerror(response) {  
        console.log("Login fallito");
        console.log(response.status);
       // _SetToken(null);                                      
    } 
       


    };

app.config(['$routeProvider', function($routeProvider) {
            $routeProvider.
            
            when('/sessionExpired', {
               templateUrl: 'sessionExpired.htm',
               controller: 'SessionExpired'
            });
            

         }]);

app.controller('SessionExpired', function($scope) {
        console.log("ciao");
            $scope.message = "You must be logged";
         });
    });
         

在 login-controller.js 中,我设置$scope.credentials = false; $scope.logout = false;隐藏您可以在 login.html 上看到的两个表单 (ng-show)。之后,login-controller.js 读取localStorage 中是否有token。如果为 false,则用户未登录,且视图必须为:在此处输入图片说明

只有第一种形式可见

var token = localStorage.getItem("token");
    if (token !== null){
     //   $window.location.href="login.html";
        $scope.credentials = false;
        $scope.logout = true;
        console.log("loggato");
    }

否则,视图必须是:

在此处输入图片说明

它工作正常。但是如果我点击菜单中的“登录/注销”,视图就会变成:在此处输入图片说明好像 login.html 没有通过日志文件 control.js 访问它!但是如果我重新加载页面,它会正常工作,直到我再次单击菜单。为什么?我该如何解决?

这很奇怪,因为最初它可以工作,登录后这是我的视图:在此处输入图片说明但是单击菜单上的“登录/注销”视图变成了这个:在此处输入图片说明

我的 login-service.js 是在 index.html 中导入的:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="UTF-8">
  <title>Ionic App</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="format-detection" content="telephone=no">
  <meta name="msapplication-tap-highlight" content="no">

  <link rel="icon" type="image/x-icon" href="assets/icon/favicon.ico">
  <link rel="manifest" href="manifest.json">
  <meta name="theme-color" content="#4e8ef7">
  
  <!-- cordova.js required for cordova apps -->
  <script src="cordova.js"></script>

  <!-- un-comment this code to enable service worker
  <script>
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.register('service-worker.js')
        .then(() => console.log('service worker installed'))
        .catch(err => console.log('Error', err));
    }
  </script>-->

  <link href="build/main.css" rel="stylesheet">

</head>
<body ng-app="UserManagement" ng-controller="LoginController" ng-fa>

  <!-- Ionic's root component and where the app will load -->
  <ion-app></ion-app>

  <!-- The polyfills js is generated during the build process -->
  <script src="build/polyfills.js"></script>

  <!-- The bundle js is generated during the build process -->
  <script src="build/main.js"></script>

</body>
    
    
<script src="angularjs/angular.js"></script> 
    <!--
<script src="angularjs/rest-services.js"></script>
<script src="angularjs/login-controller.js"></script>
    -->

      

<!-- <script src = "anguarjs/angular-route.min.js"></script> -->
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.min.js"></script>
    
<!-- Rest services -->
<script src="myjs/main-admin.js"></script>
<script src="myjs/login-controller.js"></script>
<script src="myjs/rest-services.js"></script>

    

    
</html>

阿方索·西尔维斯特里

这是因为我使用了 Angular v1 和 Ionic v2,但 Ionic v2 允许 Angular v2,它与 Angular v1 有不同的表示法。使用 Ionic v1 它可以工作

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将数据传递给侧菜单 Ionic

来自分类Dev

您如何在ionic中为侧抽屉菜单制作圆角?

来自分类Dev

如何在侧菜单 ionic 2 中删除下划线

来自分类Dev

IONIC :需要将侧菜单设置为应用程序标题的右侧

来自分类Dev

ionic2:如何在侧菜单中管理我们应用程序中的用户状态?

来自分类Dev

在开始构建/加载rootPage之前先加载配置

来自分类Dev

ionic 2中的菜单

来自分类Dev

Ionic 侧边菜单禁用链接

来自分类Dev

Ionic /如何期望缓存的视图被刷新?

来自分类Dev

刷新图表Ionic 2 Chart.js

来自分类Dev

IONIC 4 样式在视图刷新后更改

来自分类Dev

Ionic2警报与下拉菜单?

来自分类Dev

屏幕方向时关闭/打开Ionic菜单

来自分类Dev

无法在ionic2中打开菜单

来自分类Dev

无法在ionic2中打开菜单

来自分类Dev

带导航菜单的Ionic 2登录流程

来自分类Dev

在ionic 2中显示菜单栏

来自分类Dev

Ionic 2 侧边菜单未弹出

来自分类Dev

Ionic 3 侧边菜单标题中心

来自分类Dev

在 Ionic 2 中动态更新侧边菜单

来自分类Dev

ionic - 在滑动菜单关闭时触发事件

来自分类Dev

IONIC 4 - 离子菜单不与页面重叠

来自分类Dev

jQuery-侧滑菜单

来自分类Dev

离子侧菜单内容的样式

来自分类Dev

无法获取-使用Ionic刷新后出现错误

来自分类Dev

Ionic-从控制器强制刷新动画

来自分类Dev

删除元素后如何刷新Ionic 4列表项

来自分类Dev

Ionic NavController.pop() 刷新目标页面

来自分类Dev

Ionic-仅在具体页面中显示侧面菜单