无法使用 Ionic 渲染视图

Posted

技术标签:

【中文标题】无法使用 Ionic 渲染视图【英文标题】:Cannot render view using Ionic 【发布时间】:2016-01-09 13:44:33 【问题描述】:

我是第一次使用 Ionic,但遇到了一点问题。我已经阅读了文档和教程,但我无法弄清楚我做错了什么。

首先,这是我的代码(我从一个空白项目开始)。

代码

app.js(我刚刚添加了一个状态):

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic'])

.run(function($ionicPlatform) 
  $ionicPlatform.ready(function() 
    if(window.cordova && window.cordova.plugins.Keyboard) 
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    
    if(window.StatusBar) 
      StatusBar.styleDefault();
    
  );
)

.config(function($stateProvider, $urlRouterProvider) 

  $stateProvider.state('home', 
    url: '/',
    views: 
      templateUrl: 'templates/home.html'
    
  )

  $urlRouterProvider.otherwise('/');

);

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>


    <!-- compiled css output -->
    <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">

    <ion-side-menus>
      <ion-side-menu-content>
        <ion-nav-bar class="bar-positive">
          <ion-nav-back-button></ion-nav-back-button>
        </ion-nav-bar>
      </ion-side-menu-content>

      <ion-side-menu side="left">
        <ion-list>
          <ion-item menu-close ui-sref="home">
            Home
          </ion-item>
        </ion-list>
      </ion-side-menu>
    </ion-side-menus>

  </body>
</html>

我的观点(/www/templates/home.html):

<ion-view view-title="Home">
    <ion-content>
        <div>
            <div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae inventore quod nihil quia, provident porro! Nulla ad libero deserunt, ullam soluta voluptates quidem dolore harum hic consequatur quas suscipit, ab.</div>
            <div>Rerum, eum. Doloribus ullam voluptas repudiandae corrupti ipsa modi magnam, odit perferendis quam reprehenderit sapiente. Rerum impedit sequi voluptatibus porro ducimus dolor animi atque cupiditate similique praesentium. Beatae, suscipit, dolor.</div>
            <div>Quasi natus cupiditate dignissimos non sed quaerat dolorem assumenda minima consectetur, quibusdam nostrum odio molestias voluptate eaque ut fugit atque quia totam laudantium? Repellendus ad, voluptatem itaque ut dolor placeat!</div>
            <div>Quidem illo, molestias pariatur dolor laudantium et doloribus nostrum, hic quae consequuntur, nesciunt explicabo temporibus tempore possimus. Sint dolor, suscipit, iure, dolorem maiores veniam est cum eligendi iusto dolorum praesentium?</div>
            <div>Dicta nihil amet, sapiente deserunt, facilis autem. Est debitis voluptate velit dicta explicabo alias culpa laborum nesciunt mollitia delectus, quos ut eius, itaque qui temporibus laudantium iusto reprehenderit omnis incidunt.</div>
    </ion-content>
</ion-view>

问题

好的,基本上,我只想拥有一个视图(稍后再添加一些其他视图)。此时,当我为我的应用程序提供服务时,我的主视图应该会呈现,但标题(导航栏)和内容都显示。我尝试了很多东西(我的 app.js 中的语法、缓存等)。但没有任何效果。

你有什么想法吗? :) 谢谢!

【问题讨论】:

【参考方案1】:

在下面我复制了您的代码并进行了这些更改:

引入了一个管理侧边菜单结构的抽象视图(app)(相关视图是menu.html) 将主视图更改为“应用程序”的子视图

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic'])

.run(function($ionicPlatform) 
  console.log('run');
  $ionicPlatform.ready(function() 
    if (window.cordova && window.cordova.plugins.Keyboard) 
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    
    if (window.StatusBar) 
      StatusBar.styleDefault();
    
  );
)

.config(function($stateProvider, $urlRouterProvider) 
  console.log('config');
  $stateProvider.state('app', 
    url: "/app",
    abstract: true,
    templateUrl: "menu.html",
    controller: 'AppController'
  )
  .state('app.home', 
    url: "/home",
    views: 
      'menuContent': 
        templateUrl: "home.html"
      
    
  );

  $urlRouterProvider.otherwise('/app/home');

)

.controller('AppController', function($scope) 
  console.log('AppController');
);
<!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="http://code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
  <script src="http://code.ionicframework.com/nightly/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="app.js"></script>
</head>

<body ng-app="starter">
  <ion-nav-view></ion-nav-view>
  
  <script id="menu.html" type="text/ng-template">
<ion-side-menus>
  <ion-pane ion-side-menu-content>
    <ion-nav-bar class="bar-positive">
      <ion-nav-back-button class="button-clear"><i class="icon ion-chevron-left"></i> Back</ion-nav-back-button>
    </ion-nav-bar>
    <ion-nav-view name="menuContent" animation="slide-left-right"></ion-nav-view>
  </ion-pane>

  <ion-side-menu side="left">
    <header class="bar bar-header bar-positive">
      <h1 class="title">Left</h1>
    </header>
    <ion-content class="has-header">
      <ion-list>
        <ion-item nav-clear menu-close ui-sref="home">
          Home
        </ion-item>
      </ion-list>
    </ion-content>
  </ion-side-menu>
</ion-side-menus>
  </script>
  
  <script id="home.html" type="text/ng-template">
<ion-view view-title="Home">
  <ion-nav-buttons side="left">
    <button menu-toggle="left" class="button button-icon icon ion-navicon"></button>
  </ion-nav-buttons>
  <ion-content>
    <div>
      <div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae inventore quod nihil quia, provident porro! Nulla ad libero deserunt, ullam soluta voluptates quidem dolore harum hic consequatur quas suscipit, ab.</div>
      <div>Rerum, eum. Doloribus ullam voluptas repudiandae corrupti ipsa modi magnam, odit perferendis quam reprehenderit sapiente. Rerum impedit sequi voluptatibus porro ducimus dolor animi atque cupiditate similique praesentium. Beatae, suscipit, dolor.</div>
      <div>Quasi natus cupiditate dignissimos non sed quaerat dolorem assumenda minima consectetur, quibusdam nostrum odio molestias voluptate eaque ut fugit atque quia totam laudantium? Repellendus ad, voluptatem itaque ut dolor placeat!</div>
      <div>Quidem illo, molestias pariatur dolor laudantium et doloribus nostrum, hic quae consequuntur, nesciunt explicabo temporibus tempore possimus. Sint dolor, suscipit, iure, dolorem maiores veniam est cum eligendi iusto dolorum praesentium?</div>
      <div>Dicta nihil amet, sapiente deserunt, facilis autem. Est debitis voluptate velit dicta explicabo alias culpa laborum nesciunt mollitia delectus, quos ut eius, itaque qui temporibus laudantium iusto reprehenderit omnis incidunt.</div>
    </div>
  </ion-content>
</ion-view>
   </script>
</body>

</html>

【讨论】:

以上是关于无法使用 Ionic 渲染视图的主要内容,如果未能解决你的问题,请参考以下文章

Ionic 4 Tabs无法完全呈现

Spring MVC:无法渲染视图 - $ 正在视图中渲染

在 Forms UWP 中,自定义标题视图中的内容视图的后退导航无法与自定义渲染器一起正常工作

使用 Phaser.js 和 Ionic 开发游戏应用程序(缓慢/不稳定的渲染)

在渲染“页面”之前获取数据异步

Ionic 4 无法导入自定义组件不是已知元素