【问题标题】:Ionic Routing and Template not working离子路由和模板不起作用
【发布时间】:2015-06-09 00:18:29
【问题描述】:

我也是 Ionic 和 Angular UI 的新手。我正在尝试使用模板在我的 Ionic 应用程序中加载我的第一页。我的文件夹结构如下。

我想使用我的index.html 作为我所有模板的容器。我正在尝试使用list.html 作为模板,当我的应用程序首次加载时加载到index.html。我已经尝试了以下代码,但我被卡住了。

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

我的index.html只有一个 <ion-nav-view name='list-index'></ion-nav-view> 在 body 标签内。

list.html 有一些代码通过使用控制器 ListCtrl 获取一些 JSON 内容来加载列表。

应用加载为空白。

我需要指出正确的方向。我认为我离正确的解决方案太近了。非常感谢任何帮助。谢谢。

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>

链接到app.js - http://codepen.io/anon/pen/RPVJao

【问题讨论】:

  • 你有任何javascript错误吗?,你可以通过运行应用程序ionic serve并检查chrome中的开发人员工具来检查

标签: angularjs angular-ui-router ionic-framework


【解决方案1】:

您能否描述一下您的 ionic 应用程序目录结构。

应该是:

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

进入你的模板目录,你可以有“index.html & list.html”

但这会将您的路由更新为:

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

如果您将 templateURl 保留为 templateUrl: 'index.html',那么我认为您将与您的主要 index.html 冲突 .... 坏主意。

【讨论】:

  • 是的,我希望我的主文件 index.html 作为父容器。我没有一个名为模板的文件夹,所以我在文件夹 www 中都有这两个文件。我的 app.js 在 js 文件夹中。
  • 如果你没有模板文件夹,那你为什么要把 templateUrl 作为 'templates/list'。
  • 我没有,请检查我在问题上发布的代码。有必要有一个叫templates的文件夹吗?
  • 添加了文件夹结构。
  • 添加了代码。我还检查了您更新的答案。我不明白为什么必须有一个 templates 文件夹。我们不能在不创建文件夹的情况下完成这项工作吗?
猜你喜欢
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
相关资源
最近更新 更多