关于离子的角度归属途径不起作用

时间:2016-04-28 17:42:28

标签: angularjs ionic-framework

这是我的第一个关于离子和角度的应用程序,家庭路线也不起作用 ,视图未加载,但显示其他视图。 我不确定路线,因为http://localhost:8101/#/取代http://localhost:8101/

// 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 starter = angular.module('starter', ['ionic']);

starter.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();
    }
  });
})

starter.config(function($stateProvider, $urlRouterProvider){

$stateProvider
  .state('app', {
    url: '/',
    templateUrl : 'pages/home.html',
    controller : 'mainController'
  })
  .state('acerca',{
    url: '/acerca',
    templateUrl : 'pages/acerca.html',
    controller : 'aboutController'
  })
  .state('contacto',{
    url: '/contacto',
    templateUrl : 'pages/contacto.html',
    controller : 'contactController'
  });

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

starter.controller('mainController',function($scope){
  $scope.message = 'Hola,mundo';
});

starter.controller('aboutController',function($scope){
  $scope.message = 'Acerca de';
});

starter.controller('contactController',function($scope){
  $scope.message = 'Contacto';
});

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>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-route.js"></script>
  </head>
  <body ng-app="starter">
    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Ionic Blank Starter</h1>
      </ion-header-bar>
      <ion-content>
        <ion-nav-view></ion-nav-view>
      </ion-content>
    </ion-pane>
  </body>
</html>

和视图

<ion-view> 
    <ion-content> 
      <h1>Home</h1> 
      <p>{{ message }}</p>
      <a href = "#/home">Home</a>
      <a href = "#/acerca">Acerca</a>
      <a href = "#/contacto">Contacto</a>
   <ion-content>
</ion-view>

现在不显示视图内容。

1 个答案:

答案 0 :(得分:2)

将您的代码更改为使用$stateProvider$urlRouterProvider代替来自ngRouter的$routeProvider,如下所示:

// 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 starter = angular.module('starter', ['ionic']);

starter.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();
    }
  });
})

starter.config(function($stateProvider, $urlRouterProvider){

$stateProvider
  .state('app', {
    url: '/',
    templateUrl : 'home.html',
    controller : 'mainController'
  })
  .state('acerca',{
    url: '/acerca',
    templateUrl : 'pages/acerca.html',
    controller : 'aboutController'
  })
  .state('contacto',{
    url: '/contacto',
    templateUrl : 'pages/contacto.html',
    controller : 'contactController'
  });

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

starter.controller('mainController',function($scope){
  $scope.message = 'Hola,mundo';
});

starter.controller('aboutController',function($scope){
  $scope.message = 'Acerca de';
});

starter.controller('contactController',function($scope){
  $scope.message = 'Contacto';
});

此外,您现有代码中可能存在的问题是,您使用otherwise()而非redirecTo

redirectTo方法中存在拼写错误