0
votes

When I click on a link to load a ngView page, the html file does not get displayed.

I am not running the application through localhost, which I suspect could be the issue. I just opened index.html with Chrome, so perhaps ngRoute is missing some dependency?


router-app/
---index.html
---app.js
---pages/
-------home.html
-------about.html
-------contact.html

Anyways, here's my app.js

// app.js

// create the module and name it scotchApp
var scotchApp = angular.module('scotchApp', ['ngRoute']);

// configure our routes
scotchApp.config(function($routeProvider){
    $routeProvider

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

        //route for home page
        .when('/about', {
            templateURL: 'pages/about.html',
            controller: 'aboutController'
        })

        //route for home page
        .when('/contact', {
            templateURL: 'pages/contact.html',
            controller: 'contactController'
        })

        //default route
        .otherwise({
            redirectTo: '/'
        });
});

// 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 on the About page!';
});

scotchApp.controller('contactController', function($scope){
    $scope.message = 'Shoutout! I am on the Contact page!';
});

And here is index.html

<!-- index.html -->
<!DOCTYPE html>
<html ng-app='scotchApp'>
<head>

  <BASE href="/Users/Khan/Desktop/router-app/">
  <!-- SCROLLS -->
  <!-- load bootstrap and fontawesome via CDN -->
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />

  <!-- SPELLS -->
  <!-- load angular and angular route via CDN -->
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
  <script src="script.js"></script>

  <meta charset='utf-8'>
  <base href='/'>
</head>
<body>

    <!-- HEADER AND NAVBAR -->
    <header>
        <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="/">Angular Routing Example</a>
            </div>

            <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>
        </nav>
    </header>

        <!-- angular templating -->
        <!-- this is where content will be injected -->
        <div ng-view></div>

</body>
</html>

I am very curious as to what is going on, if anyone knows. Thank you for your help! :)

1

1 Answers

1
votes

Remove the leading slash from your template url,

/Pages/about.html

Should become

Pages/about.html