【问题标题】:Ionic: tab view doesn't show back button离子:选项卡视图不显示后退按钮
【发布时间】:2016-08-08 18:06:22
【问题描述】:

我有一个简单的基于选项卡的 Ionic 应用程序。在应用程序进入实际的选项卡模板之前,我有一个登录名。在进入选项卡视图之前,用户可以使用导航栏上的后退按钮在视图之间导航,这些按钮是在创建新视图时自动生成的。

但是进入标签后这不起作用。我希望标签有多个视图,并且框架应该管理导航栏,因为它有导航历史并且知道用户来自哪里。我已经在整个互联网上阅读了有关此内容的信息,但找不到示例。

这是进入“邀请”视图后应该能够返回的标签的代码:

tab-home.html

    <ion-view view-title="Angelsportverein Test e.V.">
  <ion-header-bar class="bar-positive bar-subheader">
    <h1 class="title">Mitglieder</h1>
    <div class="buttons">
      <!-- the register function navigates to the invite view -->
      <button class="button icon icon-right" ng-click="invite()">
        Einladen
      </button>
    </div>
  </ion-header-bar>
  <ion-content class="has-subheader">
    <ion-list>
      <ion-item ng-repeat="item in items">
        Item {{ item.id }}
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>

标签主控制器:

.controller('HomeCtrl', function($scope, $state) {
  $scope.invite = function() {
    console.log('Invite');
    $state.go('invite');
  };
})

app.js(请注意,注册、登录和忘记密码之间的导航完美无缺)

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider) {

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider
    //login states
    .state('signin', {
      url: '/sign-in',
      templateUrl: 'templates/sign-in.html',
      controller: 'SignInCtrl'
    })
    .state('forgotpassword', {
      url: '/forgot-password',
      templateUrl: 'templates/forgot-password.html'
    })
    .state('register', {
      url: '/register',
      templateUrl: 'templates/register.html',
      controller: 'RegisterCtrl'
    })

  // setup an abstract state for the tabs directive
    .state('tab', {
    url: '/tab',
    abstract: true,
    templateUrl: 'templates/tabs.html'
  })

  // Each tab has its own nav history stack:
  .state('tab.home', {
    url: '/home',
    views: {
      'tab-home': {
        templateUrl: 'templates/tab-home.html',
        controller: 'HomeCtrl'
      }
    }
  })
    .state('invite', {
      url: '/invite',
      templateUrl: 'templates/invite.html',
      controller: 'InviteCtrl'
    })
  .state('tab.kalender', {
      url: '/kalender',
      views: {
        'tab-kalender': {
          templateUrl: 'templates/tab-kalender.html',
          controller: 'KalenderCtrl'
        }
      }
    })
    .state('tab.chat-detail', {
      url: '/kalender/:chatId',
      views: {
        'tab-chats': {
          templateUrl: 'templates/chat-detail.html',
          controller: 'ChatDetailCtrl'
        }
      }
    })


  .state('tab.fangbuch', {
    url: '/fangbuch',
    views: {
      'tab-fangbuch': {
        templateUrl: 'templates/tab-fangbuch.html',
        controller: 'FangbuchCtrl'
      }
    }
  });

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/sign-in');

});

tabs.html

<!--
Create tabs with an icon and label, using the tabs-positive style.
Each tab's child <ion-nav-view> directive will have its own
navigation history that also transitions its views in and out.
-->
<ion-tabs class="tabs-icon-top tabs-color-active-positive">

  <!-- Dashboard Tab -->
  <ion-tab title="Home" icon-off="ion-home" icon-on="ion-home" href="#/tab/home">
    <ion-nav-view name="tab-home"></ion-nav-view>
  </ion-tab>

  <!-- Kalender Tab -->
  <ion-tab title="Kalender" icon-off="ion-calendar" icon-on="ion-calendar" href="#/tab/kalender">
    <ion-nav-view name="tab-kalender"></ion-nav-view>
  </ion-tab>

  <!-- Fangbuch Tab -->
  <ion-tab title="Fangbuch" icon-off="ion-folder" icon-on="ion-folder" href="#/tab/fangbuch">
    <ion-nav-view name="tab-fangbuch"></ion-nav-view>
  </ion-tab>


</ion-tabs>

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>
  </head>
  <body ng-app="starter">
    <!--
      The nav bar that will be updated as we navigate between views.
    -->
    <ion-nav-bar class="bar-stable">
      <ion-nav-back-button>
      </ion-nav-back-button>
    </ion-nav-bar>
    <!--
      The views will be rendered in the <ion-nav-view> directive below
      Templates are in the /templates folder (but you could also
      have templates inline in this html file if you'd like).
    -->
    <ion-nav-view></ion-nav-view>


  </body>
</html>

【问题讨论】:

    标签: tabs ionic-framework ionic-view


    【解决方案1】:

    它不起作用,因为您的 invite 状态的状态配置不正确。您已将 invite 指定为 tab 状态的同级,但它应该是 tab.home 的子级,以便 ion-nav-bar 指令自动生成后退按钮并处理历史记录。您还需要在命名选项卡tab-home 中呈现invite 视图。如下更改您的状态配置,您的预期行为应该会起作用:

    .state('tab.invite', {
      url: '/invite',
      views: {
        'tab-home': {
          templateUrl: 'templates/invite.html',
          controller: 'InviteCtrl'
        }
      }
    })
    

    【讨论】:

    • 有效!现在我了解整个系统的工作原理,非常感谢!还有一件事:箭头图标旁边的导航栏中总是有前一个视图的名称。现在它只显示“返回”,我可以以某种方式更改它吗?
    • 您要删除该名称吗?在这种情况下,您可以将以下内容用于您的app.config 方法:$ionicConfigProvider.backButton.previousTitleText(false); $ionicConfigProvider.backButton.text(' '); 另外,如果您的问题已解决,请接受答案和(或)投票:)
    • 那么这应该工作。将方法调用$ionicConfigProvider.backButton.previousTitleText(true) 添加到您的app.config
    • 不,它已经工作了,我的错误是前一个视图的名称太长了。我将把它移到导航栏下方的一个简单文本中,并将其替换为“主页”。非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 2013-05-13
    • 2015-01-14
    相关资源
    最近更新 更多