10

我有一个非常奇怪的错误,我已经设置了路线和控制器。现在我只有一个没有错误的空白页?

索引.html;

<!DOCTYPE html>
<html ng-app="RekenTalent" lang="nl">
    <head>
        <title>Rekentalent.nl: Ben jij een talent in Rekenen?</title>
        <!-- Stylesheets -->
        <link rel="stylesheet" type="text/css" href="/app/front/assets/stylesheets/style.css">
        <!-- AngularJS -->
        <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular-route.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular-animate.min.js"></script>
        <!-- Controllers -->
        <script src="/app/front/controllers/controller.js"></script>
        <!-- Router -->
        <script src="/app/front/router.js"></script>
    </head>

    <body ng-view></body>
</html

>

控制器.js:

/**
*  RekenTalent
*
* Copyright 2014 - Rekentalent.nl
*/
var RekenTalent = angular.module('RekenTalentControllers', []);

RekenTalent.controller('rekenCtrl', ['$scope', function($scope){

}]);

路由器.js:

var RekenTalent = angular.module('RekenTalent', [
    'ngRoute', 'RekenTalentControllers'
]);

RekenTalent.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
    when('/index', {
        templateUrl: '/templates/index.html',
        controller: 'rekenCtrl'
    }).
    otherwise({
        redirect: '/index'
    });
}]);

/templates/index.html 只说“嗨”。有谁知道为什么我得到一个空白页?它应该将我重定向到 /index.html 并且 /index.html 应该使用 /templates/index.html 作为模板。什么问题和解决方法?

更新:

当我转到 /index 时它可以工作,所以其他参数不起作用,为什么不呢?

4

3 回答 3

19

change redirect to redirectTo

And it's better to use home.template to avoid all this kind of problems, I mean consider:

You have an index.html that contains your whole included scripts, like angular, jquery , ..., and in the body tag there is ng-view, ok?

Now, if you have any templates such as welcome or whatever for index, write that template in a home.html, OK? Like this:

index.html :

<body ng-app="yourapp">
   <div ng-view>
   </div>
</body>

Home.html

  <div>
     Welcome, user. This is index.
  </div>  

Your app configuration:

yourapp.config(function($routeProvider) {
   $routeProvider
      .when('/', {
         templateUrl: 'partials/home.html',
         controller: 'homeCtrl'
      })
      .otherwise({redirectTo:'/'});
}

And it is a good practice to put all of your templates inside a partials folder, where the father folder contains index.html like this

  root 
     yourAppFolder
       1-Assets
          Css
          Js

       2-Partials
          home.html
          login.html

       3-index.html
于 2014-06-15T11:05:07.743 回答
2

找到了; 重定向应该是redirectTo;

var RekenTalent = angular.module('RekenTalent', [
    'ngRoute', 'RekenTalentControllers'
]);

RekenTalent.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
    when('/index', {
        templateUrl: '/app/front/templates/index.html',
        controller: 'rekenCtrl'
    }).
    otherwise({
        redirectTo: '/index'
    });
}]);
于 2014-06-15T10:58:52.527 回答
1

我有这个代码示例,例如:

.config(['$routeProvider', 
function($routeProvider) {
    console.log('routeando');
    //alert('otherwise' + $routeProvider.route);
    $routeProvider.
        when('/', {     templateUrl: 'home'}).
        when('/route1', {  templateUrl: 'route1'}).

        /*Without the next line, redirectTo doesnt work*/
        when('/error', {  templateUrl: 'error'}).
        otherwise({     redirectTo: '/error'});
}]);    

否则,只有当您已经有该路线的“何时”参数时才会起作用。

对不起我的英语,祝你有愉快的一天。

于 2014-11-24T12:59:16.567 回答