简体   繁体   中英

Ionic shows a blank page without any error

I am getting blank pages with Ionic. No errors show up in the chrome debugger. And when I inspect the debugger, I saw nothing inside of the ion-nav-view, which means for some reason the ui router is not working. Need help in figuring out the error.

Here is my code:

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 rel="manifest" href="manifest.json">

    <!-- 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="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>
    <script src="lib/ionic/js/angular/angular-resource.min.js"></script>
    <script src="lib/ionic/js/angular-ui/angular-ui-router.min.js"></script>
    <script src="lib/ngCordova/dist/ng-cordova.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/services.js"></script>
    <script src="js/controllers.js"></script>
  </head>

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

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">watchHours</h1>
    </ion-header-bar>
    <ion-content>
      <ion-list>
        <ion-item menu-close ui-sref="app">
          HOME 
        </ion-item>
        <ion-item menu-close ng-if="currentUser" ui-sref="app.user({id: uid})">
          {{username}}
        </ion-item>
        <ion-item menu-close ui-sref="app.search">
          TV SHOWS
        </ion-item>
        <ion-item menu-close ng-if="!currentUser" ng-click="login()">
          Log in
        </ion-item>
        <ion-item menu-close ng-if="!currentUser" ng-click="register()">
          Sign up
        </ion-item>
        <ion-item menu-close ng-if="currentUser" ng-click="logout()">
          Log out
        </ion-item>
      </ion-list>
    </ion-content>
  </ion-side-menu>
</ion-side-menus>

app.js:

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('watchHoursApp', ['ionic'])

.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);
      cordova.plugins.Keyboard.disableScroll(true);

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

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
    .state('app', {
      url: '/app',
      abstract: true,
      templateUrl: 'templates/menu.html',
      controller: 'SidemenuCtrl'
  });
  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/app');
});

controller.js:

angular.module('watchHoursApp')

.controller('SidemenuCtrl', ['$scope', '$state', '$rootScope', 'Shows', '$http', 
  '$location', '$localStorage', 'HomeServices', 'AuthFactory',
  '$ionicModal', '$ionicSideMenuDelegate', '$timeout',
  function ($scope, $state, $rootScope, Shows, $http, 
    $location, $localStorage, HomeServices,
    AuthFactory, $ionicModal, $ionicSideMenuDelegate, $timeout) {
    console.log("Hello");
  // With the new view caching in Ionic, Controllers are only called
  // when they are recreated or on app start, instead of every page change.
  // To listen for when this page is active (for example, to refresh data),
  // listen for the $ionicView.enter event:
  //$scope.$on('$ionicView.enter', function(e) {
  //});

   // Form data for the login modal
    $scope.loginData = $localStorage.getObject('userinfo','{}');
    $scope.reservation = {};
    $scope.registration = {};
    $rootScope.currentUser = false;
    $rootScope.username = '';
    $rootScope.admin = false;
    $rootScope.uid = '';
    $rootScope.isVerified = false;

    if(AuthFactory.isAuthenticated()) {
      $rootScope.currentUser = true;
      $rootScope.username = AuthFactory.getUsername();
      $rootScope.admin = AuthFactory.isAdmin();
      $rootScope.isVerified = AuthFactory.isVerified();
      $rootScope.uid = AuthFactory.uid();
    }

    // Create the login modal that we will use later
    $ionicModal.fromTemplateUrl('templates/login.html', {
        scope: $scope
    }).then(function (modal) {
        $scope.modal = modal;
    });

    // Triggered in the login modal to close it
    $scope.closeLogin = function () {
        $scope.modal.hide();
    };

    // Open the login modal
    $scope.login = function () {
        $scope.modal.show();
    };

    // Perform the login action when the user submits the login form
    $scope.doLogin = function () {
        console.log('Doing login', $scope.loginData);
        $localStorage.storeObject('userinfo',$scope.loginData);
        AuthFactory.login($scope.loginData);
        $scope.closeLogin();
    };

    $scope.logout = function() {
        AuthFactory.logout();
        $rootScope.currentUser = false;
        $rootScope.username = '';
        $rootScope.admin = false;
        $rootScope.isVerified = false;
        $rootScope.uid = '';
        $state.go("app");
    };

    // On successful login
    $rootScope.$on('login:Successful', function () {
        $rootScope.currentUser = AuthFactory.isAuthenticated();
        $rootScope.username = AuthFactory.getUsername();
        $rootScope.admin = AuthFactory.isAdmin();
        $rootScope.uid = AuthFactory.uid();
        $rootScope.isVerified = AuthFactory.isVerified();
    });

    // Create the login modal that we will use later
    $ionicModal.fromTemplateUrl('templates/register.html', {
        scope: $scope
    }).then(function (modal) {
        $scope.registerform = modal;
    });

    // Triggered in the login modal to close it
    $scope.closeRegister = function () {
        $scope.registerform.hide();
    };

    // Open the login modal
    $scope.register = function() {
        $scope.registerform.show();
    };

    // Perform the login action when the user submits the login form
    $scope.doRegister = function() {
        console.log('Doing registration', $scope.registration);
        $scope.loginData.username = $scope.registration.username;
        $scope.loginData.password = $scope.registration.password;

        AuthFactory.register($scope.registration);
        // Simulate a login delay. Remove this and replace with your login
        // code if using a login system
        $timeout(function () {
            $scope.closeRegister();
        }, 1000);
    };

    // On successful registration
    $rootScope.$on('registration:Successful', function () {
        $rootScope.currentUser = AuthFactory.isAuthenticated();
        $rootScope.username = AuthFactory.getUsername();
        $rootScope.admin = AuthFactory.isAdmin();
        $rootScope.uid = AuthFactory.uid();
        $rootScope.isVerified = AuthFactory.isVerified();
    });

    $scope.toggleLeft = function() {
        $ionicSideMenuDelegate.toggleLeft();
      };
}]);

您需要在angular应用初始化中注入ui-router模块。

angular.module('watchHoursApp', ['ionic','ui.router'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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