简体   繁体   中英

Ionic UI Router Not Working

I have created this simple Ionic app. Just try to learn how UI router works.

However, when I was running it, nothing came up. Also, nothing shows in the developer tools in Google Chrome.

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>


  </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>

app.js

var app = angular.module('starter', ['ionic']);
app.config(function($stateProvider) {
    $stateProvider
    .state('index',  {
        url: '/',
        templateUrl: 'templates/home.html'
    })

    .state('music', {
        url: '/music',
        templateUrl: 'templates/music.html'
    });
});

templates/home.html

    <script id="home" type="text/ng-template">
<!--        The title of the ion-view will be shown on the navbar-->
        <ion-view view-title="Home">
            <ion-content ng-controller="HomeCtrl">

<!--                The the content of the page -->
                <a href="#/music">Go to music page!</a>
            </ion-content>
        </ion-view>
    </script>

templates/music.html

    <script id="home" type="text/ng-template">
<!--        The title of the ion-view will be shown on the navbar-->
        <ion-view view-title="Home">
            <ion-content ng-controller="HomeCtrl">

<!--                The the content of the page -->
                <a href="#/music">Go to music page!</a>
            </ion-content>
        </ion-view>
    </script>

If home.html and music.html are really in separate files then you don't need to wrap them in the <script> tag. That's where I'd start.

EDIT: After looking at your github repo, your app.js does not call run().

Here is something that works, in app.js define your controllers and states. As you see I named my view as main.

app.controller('HomeCtrl', function ($scope) {
})

app.controller('MusicCtrl', function ($scope) {
})

app.config(function($stateProvider) {
    $stateProvider
    .state('index',  {
        url: '/',
        views: {
          'main': {
            templateUrl:  'templates/home.html',
            controller: 'HomeCtrl'
          }
        }
    })

    .state('music', {
        url: '/music',
        views: {
          'main': {
            templateUrl:  'templates/music.html',
            controller: 'MusicCtrl'
          }
        }
    });
});

in index.html, I named the view:

<ion-nav-view name="main"></ion-nav-view>

hom.html

<!--        The title of the ion-view will be shown on the navbar-->
        <ion-view view-title="Home">
            <ion-content>

<!--                The the content of the page -->
                <a href="#/music">Go to music page!</a>
            </ion-content>
        </ion-view>

music.html is similar.

Please note that for a real project I would not define controllers and states in app.js, I would have 3 files per page: state, controller and template inside a dedicated folder.

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