离子:$ urlRouterProvider.otherwise('/')无效

时间:2016-09-05 23:22:43

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


我在Ionic框架中使用Angular导航时遇到了一些麻烦。 我设置了两个页面(状态),应用程序应该在打开时显示第一个状态。但是, $ urlRouterProvider.otherwise 无效(我认为这是问题,至少)

我已经关注oficial documentation和一些tutorials,但没有任何结果可行。你能帮我吗?

var app = angular.module('famousguess', ['ionic', 'ui.router'])

app.config(function($stateProvider, $urlRouterProvider) {
	
	$stateProvider
	  .state('welcome', {
		url: '/',
		templateUrl: 'views/welcome.html',
		controller: 'WelcomeCtrl'
	  })
	  .state('grid', {
		url: '/grid/:gridid',
		templateUrl: 'views/grid.html',
		controller: 'GridCtrl'
	  });
	  
	$urlRouterProvider.otherwise('/');
});

app.controller('WelcomeCtrl', function ($scope, $state) {
	
}

app.controller('GridCtrl', function ($scope, $stateParams, $ionicHistory) {
	
}
<!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>Famous Guess</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="famousguess" id="wrapper">
  
	<ion-nav-bar class="bar-energized">
		<ion-nav-back-button class="button-clear">
		  <i class="ion-arrow-left-c"></i> Back
		</ion-nav-back-button>
	</ion-nav-bar>
	
	<ion-nav-view></ion-nav-view>


	
  </body>
</html>

这是我的模板( views / home.html

<ion-view view-title="welcome">
      <ion-content scroll="false" class="box-energized">

        <h1 id="logo">Famous Guess?</h1>

        <a nav-transition="android" href="#/grid/1">
            <button class="button button-block button-royal">
              Start!
            </button>
        </a>

        <button class="button icon-left button-block button-positive ion-social-facebook">
            Connect to Facebook
        </button>

      </ion-content>
</ion-view>

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您在配置中使用了错误的模板

$stateProvider
      .state('welcome', {
        url: '/',
        templateUrl: 'views/home.html',
        controller: 'WelcomeCtrl'
      })
      .state('grid', {
        url: '/grid/:gridid',
        templateUrl: 'views/grid.html',
        controller: 'GridCtrl'
      });

    $urlRouterProvider.otherwise('/');
});

我认为以上将会奏效!!你在tempalateurl中加载了错误的模板文件