AngularJS: ngRoute, otherwise not working

DazDylz picture DazDylz · Jun 15, 2014 · Viewed 39.4k times · Source

I have a really weird bug, I have set my routes and my controllers. Now I have just a blank page with no errors?

index.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

>

Controller.js:

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

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

}]);

Router.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'
    });
}]);

And /templates/index.html says just 'hi'. Does anyone know why I get a blank page? It should redirect me to /index.html and /index.html should use /templates/index.html as the template. Whats the problem and the fix?

UPDATE:

when I go to /index it works, so the otherwise param doesn`t work, why not?

Answer

Milad picture Milad · Jun 15, 2014

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