0

Following is the folder structure I am using for my demo application

  • css
  • fonts
  • images
  • js
  • views index.html

The code for index.html is as follows :

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Welcome to Sandhu Tutors</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<body>

    <!-- Top Navigation-->
    <nav class="nav navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Menu</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>   

                <a class="navbar-brand" href="#">ABSS</a>
            </div>

            <div id="navbar" class="navbar-collapse collapse">
                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="#">Home</a></li>
                        <li><a href="3">About Us</a></li>
                        <li><a ng-href="">First</a></li>
                        <li><a ng-href="">Second</a></li>
                    </ul>
            </div>
        </div>

    </nav>




    <!--Main view goes here -->

    <div ng-view></div>



    <footer>
          <div class="navbar  navbar-inverse navbar-fixed-bottom"> 
            <div class="container">
                <div class="navbar-text pull=left">
                    <p> ABSS © Sandhu 2017.</p>
                </div>        
            </div>
        </div>
    </footer>

    <script src="js/jquery-3.1.1.min.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/angular.min.js"></script>
    <script src="js/angular-route.js"></script>
    <script src="js/controller.js"></script>

</body>
</html>

And here goes the code for controller.js :

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


app.config(function($routeProvider){
    $routeProvider

    .when('/',{
        templateUrl : 'views/home.html',
        controller : 'homeCtrl'
    })
    .when('/first',{
        templateUrl:'views/first.html',
        controller : 'firstCtrl'
    })
    .when('/second',{
        templateUrl:'views/second.html',
        controller : 'secondCtrl'
    })
    .otherwise({
        redirect :'/'
    });
});


app.controller('homeCtrl',function($scope){
    $scope.name="Angad";
});

app.controller('firstCtrl',function($scope){
    $scope.name="Second";
});

app.controller('secondCtrl',function($scope){
    $scope.name="Second";
});

Routing doesnt seems to work here .... The only error it shows is : -Error: [$compile:tpload] -XMLHttpRequest cannot load

Apart from this if anyone could help as to how to make the desired pages open in the ng-view section on the click of the top navbar buttons ...would be highly appreciated !!

1 Answer 1

1

Your code seems to be correct. If you are using chrome as a browser use a local webserver to test your site (for example lite-server). Chrome won't allow you to load local files for security reasons. You could also try and test your application with another browser.
Your Navbar hrefs should look like:

<li><a href="#/first">About Us</a></li>

or if you are using AngularJS 1.6

<li><a href="#!/first">About Us</a></li>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot !! It helped :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.