Alexandre Couret
Alexandre Couret

Reputation: 454

AngularJS routeProvider doesn't work

I have a problem when I try to use the $routeProvider. It just doesn't work at all and I don't see any problems :

var app = angular.module('app', ['ngStorage','ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: '/index.html',
            controller: 'authCtrl'
        })
        .when('/dashboard', {
            templateUrl: '/tpl/dashboard.html',
            controller: 'mainCtrl'
        })
        .otherwise({
            redirectTo: '/'
        });
}]);

Here is index.html :

<html>
  <head>
    <title>Authenticate | BulbThings</title>
    <script src="angular/angular.min.js"></script>
    <script src="angular/ngStorage.min.js"></script>
    <script src="angular/angular-route.min.js"></script>
    <script src="js/build/app.min.js"></script>
  </head>
  <body ng-app="app">
    <div ng-show="authenticate">
      <span ng-cloak ng-show="addr" id="address">{{address}}</span>
      <input type="mail" ng-model="email" placeholder="Email"><br />
      <input type="password" ng-model="password" placeholder="Password"><br />
      <button ng-click="auth.connect(email, password)" ng-model="signin">Sign in</button><br/><br/>
    </div>
    <p>{{error}}</p>
  </body>
</html>

I set $scope.authenticate = true in the controller, and when I load the page it doesn't show up.

When I try /dashboard it returns me Cannot GET /dashboard.

I can't see what is going wrong I hope you can help me !

Thank you.

Upvotes: 3

Views: 4751

Answers (3)

Max
Max

Reputation: 185

I had a similar issue, cost me days of searching. The href was incorrect, coudln't make it work with "#home" or "home"

Here is my working code :

<html ng-app="app">

<head>
    <script src="js/angular.js"></script>
    <script src="js/angular-route.js"></script>
    <title>Testing</title>
</head>

<body>
<a href="#!/home">home</a>
<a href="#!/about">about</a>

<div ng-view="">
</div>


<script>
    var app = angular.module('app', ['ngRoute'])
    app.config(['$routeProvider', function ($routeProvider) {
        $routeProvider
                .when('/home', {
                    templateUrl: 'views/home.html'
                })
                .when('/about', {
                    templateUrl: 'views/about.html'
                })


    }]);
</script>

Upvotes: 0

Facundo Pedrazzini
Facundo Pedrazzini

Reputation: 1486

2 things, first you need the ng-view to render the view

<div ng-view=""></div>

And second, if you are getting "Cannot GET /dashboard" is because in angular the routes are in the hash, example:

yourdomain.com/#/dashboard, and not yourdomain.com/dashboard. So here you have a link if you want to go to dashboard:

<a ng-href="#/dashboard">Dashboard</a>

Upvotes: 3

Riad Baghbanli
Riad Baghbanli

Reputation: 3319

You are missing tag with ng-view directive.

Upvotes: 0

Related Questions