Ionic UI路由器不工作

Ionic UI Router Not Working

本文关键字:工作 路由器 UI Ionic      更新时间:2023-09-26

我已经创建了这个简单的Ionic应用程序。只是尝试学习UI路由器是如何工作的。

然而,当我运行它时,什么也没有出现。此外,在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'
    });
});

模板/home。

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

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

编辑:在查看您的github repo后,您的app.js没有调用run()。

这是一些工作,在app.js定义你的控制器和状态。如你所见,我将我的视图命名为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'
          }
        }
    });
});
在index.html中,我将视图命名为:
<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类似。

请注意,对于一个真正的项目,我不会在app.js中定义控制器和状态,我会在每个页面有3个文件:状态,控制器和模板在一个专用文件夹内。

如果home.html和music.html确实在不同的文件中,那么您不需要将它们包装在<script>标记中。从这里开始。