在ionic中设置默认视图
Set default view in ionic
我被困在一些非常简单的事情上,似乎无法弄清楚我做错了什么。我在 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'
var app = angular.module('starter', ['ionic'])
app.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();
}
});
})
app.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/')
$stateProvider.state('home_screen', {
url:'/',
templateUrl: 'home_screen.html'
})
})
home_screen.html
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Home Screen</h1>
</ion-header-bar>
<ion-content>
<div class="spacer" style="height: 100px;"></div>
<div class="col col-33 col-offset-33">
<button class="button button-block button-balanced">Play</button>
</div>
<div class="spacer" style="height: 100px;"></div>
<div class="col col-33 col-offset-33">
<a class="button button-outline button-block button-balanced" href="#/app/about">About</a>
</div>
</ion-content>
</ion-pane>
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">
<ion-nav-view></ion-nav-view>
</body>
</html>
当我将 home_screen.html 包含在 index.html 中时,
home_screen.html 会正常工作,但当我尝试将其设置为默认视图时,它不会工作。我尝试按照这里的教程进行操作:
http://learn.ionicframework.com/formulas/navigation-and-routing-part-1/
如有任何帮助,我们将不胜感激。
实际上,您必须取消注释 index.html 文件中的 ion-nav-view
。你甚至不必给它起个名字。尝试将此添加到您的 index.html 文件中:
...
<body ng-app="starter">
<ion-nav-view></ion-nav-view>
</body>
...
要开始使用 ionic,我还建议查看其他入门项目,例如 here 中描述的选项卡或侧边菜单。
我被困在一些非常简单的事情上,似乎无法弄清楚我做错了什么。我在 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'
var app = angular.module('starter', ['ionic'])
app.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();
}
});
})
app.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/')
$stateProvider.state('home_screen', {
url:'/',
templateUrl: 'home_screen.html'
})
})
home_screen.html
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Home Screen</h1>
</ion-header-bar>
<ion-content>
<div class="spacer" style="height: 100px;"></div>
<div class="col col-33 col-offset-33">
<button class="button button-block button-balanced">Play</button>
</div>
<div class="spacer" style="height: 100px;"></div>
<div class="col col-33 col-offset-33">
<a class="button button-outline button-block button-balanced" href="#/app/about">About</a>
</div>
</ion-content>
</ion-pane>
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">
<ion-nav-view></ion-nav-view>
</body>
</html>
当我将 home_screen.html 包含在 index.html 中时,
home_screen.html 会正常工作,但当我尝试将其设置为默认视图时,它不会工作。我尝试按照这里的教程进行操作:
http://learn.ionicframework.com/formulas/navigation-and-routing-part-1/
如有任何帮助,我们将不胜感激。
实际上,您必须取消注释 index.html 文件中的 ion-nav-view
。你甚至不必给它起个名字。尝试将此添加到您的 index.html 文件中:
...
<body ng-app="starter">
<ion-nav-view></ion-nav-view>
</body>
...
要开始使用 ionic,我还建议查看其他入门项目,例如 here 中描述的选项卡或侧边菜单。