Search code examples
angularjsangularjs-directiveng-viewangularjs-ng-route

Angularjs routing with ngroute and ngview does not work for me.Clicking on a link does not load the intended page


There are some 3 links(Home, About, Contact) in the index.html file. On clicking them, I should be able to display new messages/html(home.html, contact.html, about.html) files as defined in script js. But that does not happen.

There are otherwise no errors while doing an "inspect element' in chrome.

The url on clicking on contact is : file:///C:/Users/Sonali%20Sinha/Desktop/demo/omg/index.html#contact

The code files are as below.

index.html

<!DOCTYPE html>
<html ng-app="scotchApp">
<head>    
<script src="angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.js"></script>
<script src="script.js"></script>
</head>
<body ng-controller="mainController">
    <ul class="nav navbar-nav navbar-right">
        <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
        <li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
        <li><a href="#contact"><i class="fa fa-comment"></i> Contact</a>   </li>
     </ul>
<div id="main">
 {{ message }}
<div ng-view></div>
</div>
</body>
</html>

script.js

scotchApp.config(function($routeProvider,$locationProvider) {
        $routeProvider

            // route for the home page
            .when('/', {
                templateUrl : '/home.html',
                controller  : 'mainController'
            })

            // route for the about page
            .when('/about', {
                templateUrl : '/about.html',
                controller  : 'aboutController'
            })

            // route for the contact page
            .when('/contact', {
                templateUrl : '/contact.html',
                controller  : 'contactController'
            });
            $locationProvider.html5Mode({
                enabled: true,
                requireBase: false
            });
    });

    // create the controller and inject Angular's $scope
    scotchApp.controller('mainController', function($scope) {
        // create a message to display in our view
        $scope.message = 'Everyone come and see how good I look!';
    });

    scotchApp.controller('aboutController', function($scope) {
        $scope.message = 'Look! I am an about page.';
    });

    scotchApp.controller('contactController', function($scope) {
        $scope.message = 'Contact us! JK. This is just a demo.';
    });

home.html

<div>
<h1>Home Page</h1>
<p>{{ message }}</p>
</div>

contact.html

<div>
<h1>Contact Page</h1>
<p>{{ message }}</p>
</div>

about.html

<div>
<h1>About Page</h1>
<p>{{ message }}</p>
</div>

The directory structure is as below.

demo/omg/index.html All files are inside omg.

Guys, I am stuck with this for a day now.:-( I am just a day old in angularjs. Please be kind and help me out. Thanks in advance! :-)


Solution

  • You need to do 2 things :

    • Run it behind an Apache as JB mentioned above.
    • Remove the locationProvider block, it worked for me.