我是离子的新手,我试图使用此代码导航到另一个页面:
HTML index.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 rel="manifest" href="manifest.json">
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="Humanity" ng-controller="main">
<div style="with:100%;">
<img src="img/loginlogo.png" style="display:block;margin:0 auto;">
</div> <br>
<div class="list">
<label class="item item-input item-floating-label">
<span class="input-label">Email</span>
<input type="text" placeholder="First Name">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Password</span>
<input type="text" placeholder="Last Name">
</label>
</div>
<button ng-click="test();" class="button button-block icon-right ion-chevron-right button-balanced" style="width:90%;display:block;margin:0 auto;">
Sign in
</button><br>
<button class="button button-block icon-right ion-chevron-right button-positive" style="width:90%;display:block;margin:0 auto;">
Register
</button>
</body>
</html>
js app.js:
var app = angular.module('Humanity', ['ionic']);
app.controller('main', function ($scope,$state, $ionicModal, $location) {
var route = angular.module('route', ["ui.router"]);
app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home1', {
url: "templates/home1",
templateUrl: "templates/test.html"
})
});
$scope.test = function() {
$state.go('home1');
};
});
我不知道问题出在哪里,我试图搜索,没找到任何有效的解决方案。我验证了注射器,我尝试了$ location。没有什么工作。
有什么想法吗?
答案 0 :(得分:1)
尝试从控制器外部初始化路由,如下所示。
var app = angular.module('Humanity', ['ionic']);
app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home1', {
url: "templates/home1",
templateUrl: "templates/test.html"
})
});
app.controller('main', function($scope, $state, $ionicModal, $location) {
$scope.test = function() {
$state.go('home1');
};
});
答案 1 :(得分:0)
你忘了把指令(ui-view)放在可以注入模板的地方......比如:
<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 rel="manifest" href="manifest.json">
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="cordova.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="Humanity" ng-controller="main">
<div style="with:100%;">
<img src="img/loginlogo.png" style="display:block;margin:0 auto;">
</div> <br>
<div class="list">
<label class="item item-input item-floating-label">
<span class="input-label">Email</span>
<input type="text" placeholder="First Name">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Password</span>
<input type="text" placeholder="Last Name">
</label>
</div>
<button ng-click="test();" class="button button-block icon-right ion-chevron-right button-balanced" style="width:90%;display:block;margin:0 auto;">
Sign in
</button><br>
<button class="button button-block icon-right ion-chevron-right button-positive" style="width:90%;display:block;margin:0 auto;">
Register
</button>
<ui-view></ui-view> <!-- // HERE IS INJECTED YOUR TPL -->
</body>
</html>