Guillem Vicens
Guillem Vicens

Reputation: 3996

Ionic side menu and tabs with nested views: content of tab nested views not showing

I am new to both AngularJS and Ionic.

I am creating an app that has both a sidemenu and tabs inside one of the menu's options. The tabs should render its content by using a nested view, so no plain HTML here.

I can navigate between the sidemenu options and all works and gets rendered properly. The problem comes when I enter the menu option that hosts the tabs.

While the tabs navigation panel renders properly and I can navigate between the tabs, somehow the tab's content is created but remains invisible. If I do an ionic serve and inspect the generated HTML with Firebug or similar I can see the content of the nested view generated. It is just I do not see it on the browser. This happens to me in both Chromium and Firefox so browser should not be the problem. Same happens when trying with the Android emulator.

I first thought of a routing problem, but that does not make too much sense to me, since the content is generated (just invisible).

I have created a Plunkr with my code reduced to the minimum in order to reproduce the problem. Anyway, my (relevant) code is as following:

index.html

<!DOCTYPE html>
<html>

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

  <link rel="stylesheet" type="text/css" href="http://code.ionicframework.com/nightly/css/ionic.css">

  <link href="app.css" rel="stylesheet">

  <script src="http://code.ionicframework.com/nightly/js/ionic.bundle.js"></script>

    <!-- 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="app.js"></script>
    <script src="services.js"></script>
    <script src="controllers.js"></script>
  </head>

  <body ng-app="starter">
    <ion-nav-view></ion-nav-view>
  </body>

</html>

app.js

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

.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.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

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

  .state('app', {
    url: "/app",
    abstract: true,
    templateUrl: "menu.html",
    controller: 'AppCtrl'
  })

  .state('app.questionsAnswers', {
    url: "/questionsAnswers",
//    abstract: true,
    views: {
      'menuContent': {
        templateUrl: "questionsAnswers.html"//,
//        controller: 'questionsAnswersCtrl'
      }
    }
  })

  .state('app.questionsAnswers.recent', {
    url: "/recent",
    views: {
      'recent-questions': {
        templateUrl: "recentQuestionsAnswers.html",
        controller: 'recentQuestionsAnswersCtrl'
      }
    }
  })

  .state('app.questionsAnswers.mostVoted', {
    url: "/mostVoted",
    views: {
      'most-voted-questions': {
        templateUrl: "mostVotedQuestionsAnswers.html",
        controller: 'mostVotedQuestionsAnswersCtrl'
      }
    }
  })

  .state('app.questionsAnswers.random', {
    url: "/random",
    views: {
      'random-questions': {
        templateUrl: "randomQuestionsAnswers.html",
        controller: 'randomQuestionsAnswersCtrl'
      }
    }
  })


    .state('app.foo', {
      url: "/foo",
      views: {
        'menuContent': {
          templateUrl: "foo.html",
          controller: 'fooCtrl'
        }
      }
    });
  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/app/foo');
});

menu.html

<ion-side-menus enable-menu-with-back-views="false">
  <ion-side-menu-content>
    <ion-nav-bar class="bar-stable">
      <ion-nav-back-button>
      </ion-nav-back-button>

      <ion-nav-buttons side="left">
        <button class="button button-icon button-clear ion-navicon" menu-toggle="left">
        </button>
      </ion-nav-buttons>
    </ion-nav-bar>
    <ion-nav-view name="menuContent"></ion-nav-view>
  </ion-side-menu-content>

  <ion-side-menu side="left">
    <ion-header-bar class="bar-stable">
      <h1 class="title">Menu</h1>
    </ion-header-bar>
    <ion-content>
      <ion-list>
                <ion-item nav-clear menu-close href="#/app/questionsAnswers">
          Questions & Answers
        </ion-item>
        <ion-item nav-clear menu-close href="#/app/foo">
          Foo
        </ion-item>        
      </ion-list>
    </ion-content>
  </ion-side-menu>
</ion-side-menus>

questionsAnswers.html

<ion-view view-title="Questions & Answers">
  <ion-content>
    <h1>Questions & Answers</h1>
        <ion-tabs class="tabs-positive tabs-icon-only">

          <ion-tab title="Recent" ui-sref="app.questionsAnswers.recent" icon-on="ion-ios-clock" icon-off="ion-ios-clock-outline">
                <ion-nav-view name="recent-questions"></ion-nav-view>
          </ion-tab>

          <ion-tab title="Most voted" ui-sref="app.questionsAnswers.mostVoted" icon-on="ion-arrow-up-c" icon-off="ion-arrow-up-a">
                <ion-nav-view name="most-voted-questions"></ion-nav-view>
          </ion-tab>

          <ion-tab title="Random" ui-sref="app.questionsAnswers.random" icon-on="ion-help-circled" icon-off="ion-help">
                <ion-nav-view name="random-questions"></ion-nav-view>
          </ion-tab>

        </ion-tabs>
  </ion-content>
</ion-view>

The rest of the HTML files in my SSCCE are just plain views of the type:

<ion-view view-title="foo">
  <ion-content>
    <ion-list>
      foo content
    </ion-list>
  </ion-content>
</ion-view>

The controllers.js is not really relevant in the example since all controllers do basically nothing (of course in the real app they all have their own functionality).

I have read a lot of questions and answers both in SO and other webs, but can not seem to find whatever I am doing wrong. Question is: why does the nested view in my tabs render but remain invisible?

Upvotes: 2

Views: 5557

Answers (1)

LeftyX
LeftyX

Reputation: 35587

Your ion-view Questions & Answers (questionAnswers.html) cannot contain a <ion-content> element cause you're already setting the <ion-content> in each sub-view.

EX:

<ion-view view-title="Most voted questions and answers">
  <ion-content>
    <h1>Most voted Questions</h1>
  </ion-content>
</ion-view>

So your questionAnswers.html should be:

<ion-view view-title="Questions & Answers">
        <ion-tabs class="tabs-positive tabs-icon-only tabs-top">

          <ion-tab title="Recent" ui-sref="app.questionsAnswers.recent" icon-on="ion-ios-clock" icon-off="ion-ios-clock-outline">
                <ion-nav-view name="recent-questions">
                </ion-nav-view>
          </ion-tab>

          <ion-tab title="Most voted" ui-sref="app.questionsAnswers.mostVoted" icon-on="ion-arrow-up-c" icon-off="ion-arrow-up-a">
                <ion-nav-view name="most-voted-questions"></ion-nav-view>
          </ion-tab>

          <ion-tab title="Random" ui-sref="app.questionsAnswers.random" icon-on="ion-help-circled" icon-off="ion-help">
                <ion-nav-view name="random-questions"></ion-nav-view>
          </ion-tab>

        </ion-tabs>
</ion-view>

This is the plunker.

Upvotes: 4

Related Questions