簡體   English   中英

離子UI路由器不起作用

[英]Ionic UI Router Not Working

我已經創建了這個簡單的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'
    });
});

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>

如果home.html和music.html確實位於單獨的文件中,則無需將它們包裝在<script>標記中。 那就是我要開始的地方。

編輯:在查看您的github存儲庫后,您的app.js不會調用run()。

這是可行的,在app.js中定義了控制器和狀態。 如您所見,我將視圖命名為主視圖。

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個文件:專用文件夾中的狀態,控制器和模板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM