离子角状态路由不能正常工作

时间:2016-01-31 14:14:58

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

状态路由对我来说效果不佳。 第一个名为主页的链接会自动加载,如果我点击主页中的链接,它应该转到搜索页面,而是将数据附加到主页本身。所以我在同一页面上看到了主页和搜索页面数据。

的index.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="js/index.js"></script>
<!--<base href="/">-->
</head>
<body ng-app="hello">
    <ion-nav-view></ion-nav-view>
</body>
</html>

index.js

angular.module('hello', ['ionic'])

.config(function($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise('home');

  $stateProvider.state('home', {
        url: "/home",
        templateUrl: "/home.html"

            });
  $stateProvider.state('search', {
        url: "/search",
        templateUrl: "/search.html"
            });
    });

home.html的

<a href="#/search">Go to Search</a>

search.html

<div> you are in Search Screen</div>

我创建了一个Plunker,但由于某种原因我没有看到任何输出http://plnkr.co/edit/UpnS01Oy2MLqHvKSAPST

1 个答案:

答案 0 :(得分:0)

您的代码中存在问题:视图必须包含在<ion-view>元素中。

<ion-view view-title="Home">
  <ion-content>
    <a href="#/search">Go to Search</a>
  </ion-content>
</ion-view>

以下是带有更正的Plunker:

http://plnkr.co/edit/ONAIfq26uwN7ZoYQtY0N?p=preview

PS:在Ionic build之前注意添加对cordova.js的引用。