Dũng Nguyễn
Dũng Nguyễn

Reputation: 13

ng-route in AngularJS doesn't work

I am practicing AngularJS and have one problem: ng-route doesn't work even though I added angular-route.js.

Here is my app.js file:

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


app.config(['$routeProvider', function($routeProvider) {
    $routeProvider
    .when('/', {
        templateUrl: 'views/intro.html'
    })
    .when('game', {
        templateUrl: 'views/game.html',
        controller: 'TableController'
    })
    .when('about', {
        templateUrl: 'views/about.html'
    })
    .otherwise({
        redirectTo: '/'
    });
}]);

and here is my index.html

<!DOCTYPE html>
<html>
<head>
    <title>Color Picking</title>
    <meta name="author" content="pexea12">
    <meta charset="utf-8">
    <meta name="viewport" content="device=device-width, initial-scale=1.0">

    <!-- CSS -->
    <link rel="stylesheet" href="css/style.css">

    <!-- Bootstrap -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/bootstrap-theme.min.css">

    <!-- Script -->
    <script src="js/bootstrap/jquery-1.11.3.min.js"></script>
    <script src="js/bootstrap/bootstrap.min.js"></script>

</head>
<body ng-app="app">
    <header>
        <br>
        <h2>Color Picking</h2>
        <br>
    </header>

    <div id="main">
       <div ng-view></div>
    </div> <!-- main -->

    <!-- AngularJS -->
    <script src="js/vendor/angular.min.js"></script>
    <script src="js/vendor/angular-route.js"></script>
    <script src="js/app.js"></script>

    <!-- Services -->
    <script src="js/services/ColorService.js"></script>

    <!-- Factories -->
    <script src="js/factories/RandomFactory.js"></script>

    <!-- Controllers -->
    <script src="js/controllers/TableController.js"></script>

</body>
</html>

My folder tree is: css (css files, Bootstraps) js (app.js, controllers, services, factories, ...) views

My website works with http://localhost:8080/ but doesn't work with localhost:8080/about or localhost:8080/game.

I am really stuck at this point and can't find the solution.

Upvotes: 1

Views: 438

Answers (1)

code
code

Reputation: 1137

I think your're having a problem after your .when, should be :

$routeProvider
.when('/', {
    templateUrl: 'views/intro.html'
})
.when('/game', {
    templateUrl: 'views/game.html',
    controller: 'TableController'
})
.when('/about', {
    templateUrl: 'views/about.html'
})
.otherwise({
    redirectTo: '/'
});

You're missing the / in your routes

Upvotes: 1

Related Questions