Ionic Routing and Template not working

2019-09-08 19:15发布

问题:

I am new to Ionic and Angular UI also. I am trying to use templates to load my first page in my Ionic app. My folder structure is as following.

I want to use my index.html as the container for all my templates. I am trying to use list.html as the template the loads into the index.html when my app first loads. I have tried the following code and I am stuck.

.config(function($urlRouterProvider, $stateProvider){

  $stateProvider
    .state('index', {
      url: '/',
      abstract: true,
      templateUrl: 'index.html'
    })
    .state('index.list', {
      url: '/list',
      views: {
        'list-index' : {
          templateUrl: 'list.html',
          controller: 'ListCtrl'
        }
      }
    })

    $urlRouterProvider.otherwise('/list');
})

My index.html has nothing but an <ion-nav-view name='list-index'></ion-nav-view> inside the body tag.

The list.html has some code that loads a list by fetching some JSON content using conotroller ListCtrl.

The app loads up blank.

I need to be pointed in the right direction. I think that I am too close to the correct solution. Any help is highly appreciated. Thanks.

Adding code for 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="lib/ngCordova/dist/ng-cordova.js"></script>
    <script src="cordova.js"></script>
    <script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.4.2.min.js"></script>
    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">

    <ion-nav-view name='list-index'></ion-nav-view>

  </body>
</html>

Link to app.js - http://codepen.io/anon/pen/RPVJao

回答1:

Can you please describe your ionic app directory structure.

It shall be :

PROJECT
 - hooks
 - platforms
 - www
   - js <=== this dir includes all your js in ionic structure. 
   - templates/ <== this is a dir that includes all html templates in ionic structure
   - index.html <== THIS IS THE STARTING POINT OF YOUR PROJECT, CONTAINING angularJS script and ng-app for exemple

into your template directory, you can have "index.html & list.html"

but this shall update your routing as :

$stateProvider
    .state('index', {
      url: '/',
      abstract: true,
      templateUrl: 'templates/index.html'
    })
    .state('index.list', {
      url: '/list',
      views: {
        'list-index' : {
          templateUrl: 'templates/list.html',
          controller: 'ListCtrl'
        }
      }
    })

if you keep your templateURl as templateUrl: 'index.html' then, I assume that you 'll be in conflict with your main index.html .... bad idea.