0

I am trying to create a one-page website that loads views in based on buttons clicked on a bootstrap navbar. However, The content of the page does not show up. Can anyone tell me what is going wrong?

Okay, so I have this index.html:

  <!DOCTYPE html>

<!-- define angular app -->
<html ng-app="scotchApp">

<head>
  <!-- SCROLLS -->
  <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 -->
  <script src="http://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>
  <script src="controllers/userdetailsController.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
  <script src="utils/basicUtils.js"></script>
</head>

<!-- define angular controller -->
<body ng-controller="mainController">

<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> User</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>

<div id="main">

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

</div>



</body>

</html>

This script.js:

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

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

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

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

            // route for the contact page
            .when('/contact', {
                templateUrl : 'pages/contact.html',
                controller  : 'contactController'
            });
    });

     //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.';
    });

And this home.html:

<!doctype html>
<html >
<head>
    <title>User Details</title>
    <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
    <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-route.min.js"></script>
    <script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src = "../controllers/userdetailsController.js"></script>
    <script src = "../utils/basicUtils.js"></script>
</head>
<body>
<div ng-app="hyprPortal" ng-controller="UserRegCtrl">
    <!--<form ng-submit = "add(USERID)">
        User ID:<br>
        <input type = "text" name = "USERID" ng-model = "USERID"><br>
        <input type = "submit" value = "Submit">
    </form>-->
    <table border="1">
        <tr>
            <td>{{returnName}}</td>
            <td>{{AID}}</td>
        </tr>
    </table>
</div>
</body>
</html>
3
  • You don't need the html, body, and script tags inside home.html. You only need the content. All script tags should be defined in index.html. Commented Jun 13, 2016 at 13:34
  • I have created a plunker for you , hope this will help you. plnkr.co/edit/Bvo3LkVmGNGZWxF4M66m?p=preview Commented Jun 13, 2016 at 13:38
  • Got rid of all that, still didn't work. Commented Jun 13, 2016 at 13:39

1 Answer 1

1

There are few issues with your code.

1: The home.html need not to have all the head, body, parts. 2: the module name should be scothApp and should only be present as ng-app = "scothApp" in the index.html 3: your route already tells that the controller for Home.html is mainController so you need not to add another cotroller using ng-controller= "UserRegCtrl" in the home.html.

check out this plunker based on your code.

https://plnkr.co/edit/Bvo3LkVmGNGZWxF4M66m?p=preview

var scotchApp = angular.module('scotchApp', ['ngRoute']);
Sign up to request clarification or add additional context in comments.

2 Comments

what if I want the other controller instead?
change it in the route itself. .when('/', { templateUrl : 'home.html', controller : 'otherController' })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.