Search code examples
angularjsangularjs-routing

What is the workflow of angular routing?


I'm working with angula rjs routin , everything works fine, but the routing is giving me a weird problem. When i click a link on my index.html page so the url look like the following

http://localhost:0000/Index.html#%2Fhome

Where as it should look like:

http://localhost:0000/Index.html#/home

When I make a change to my routing function from "/home" to only "/" my application works well, I don't understand where I'm going wrong following is my routing file:

var app = angular
            .module("Demo", ["ngRoute"])
            .config(function ($routeProvider, $locationProvider) {
                $locationProvider.html5Mode(true);
                $routeProvider
                    .when("/home", {
                        templateUrl: "Templates/home.html",
                        controller: "homeController"
                    })
                    .when("/courses", {
                        templateUrl: "Templates/courses.html",
                        controller: "coursesController"
                    })
                    .when("/students", {
                        templateUrl: "Templates/students.html",
                        controller: "studentsController"
                    })
            })
            .controller("homeController", function ($scope) {
                $scope.message = "Home Page";
            })
            .controller("coursesController", function ($scope) {
                $scope.courses = ["C#", "VB.NET", "ASP.NET", "SQL Server"];
            })
             .controller("studentsController", function ($scope, $http) {
                 $http.get("StudentService.asmx/GetAllStudents")
                                        .then(function (response) {
                                            $scope.students = response.data;
                                        })
             })

And following is my index.html:

<!DOCTYPE html>
<html ng-app="Demo">
<head>
    <title></title>
    <script src="scripts/angular.min.js"></script>
    <script src="scripts/angular-route.min.js"></script>
    <link href="Styles.css" rel="stylesheet" />
    <script src="scripts/Script.js"></script>
    <base href="/"/>
    <meta charset="utf-8" />
</head>
<body>
    <table style="font-family: Arial">
        <tr>
            <td colspan="2" class="header">
                <h1>
                    WebSite Header
                </h1>
            </td>
        </tr>
        <tr>
            <td class="leftMenu">
                <a href="#/home">Home</a>
                <a href="#/courses">Courses</a>
                <a href="#/students">Students</a>
            </td>
            <td class="mainContent">
                <ng-view></ng-view>
            </td>
        </tr>
        <tr>
            <td colspan="2" class="footer">
                <b>Website Footer</b>
            </td>
        </tr>
    </table>
</body>
</html>

Solution

  • So, I followed the given comments and updated the index.html as follows, which worked for me. Thanks!

    <!DOCTYPE html>
    <html ng-app="Demo">
    <head>
        <title></title>
        <script src="scripts/angular.min.js"></script>
        <script src="scripts/angular-route.min.js"></script>
        <script>document.write('<base href="' + document.location + '" />');</script>
        <link href="Styles.css" rel="stylesheet" />
        <script src="scripts/Script.js"></script>
        <base href="/"/>
        <meta charset="utf-8" />
    </head>
    <body>
        <table style="font-family: Arial">
            <tr>
                <td colspan="2" class="header">
                    <h1>
                        WebSite Header
                    </h1>
                </td>
            </tr>
            <tr>
                <td class="leftMenu">
                    <a href="/home">Home</a>
                    <a href="/courses">Courses</a>
                    <a href="/students">Students</a>
                </td>
                <td class="mainContent">
                    <ng-view></ng-view>
                </td>
            </tr>
            <tr>
                <td colspan="2" class="footer">
                    <b>Website Footer</b>
                </td>
            </tr>
        </table>
    </body>
    </html>