离子UI路由器不工作

时间:2015-07-10 20:53:27

标签: javascript html angularjs ionic

我创建了这个简单的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.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>

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

2 个答案:

答案 0 :(得分:0)

如果home.html和music.html确实位于单独的文件中,那么您无需将它们包装在<script>标记中。那是我开始的地方。

答案 1 :(得分:0)

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

这是有用的东西,在app.js中定义你的控制器和状态。 如你所见,我将我的观点命名为主要观点。

self
在index.html中,我将视图命名为:

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'
          }
        }
    });
});

hom.html

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

music.html很相似。

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

相关问题