0

I am learning how to implement routing in AngularJS. Everything else is working fine but the views are not getting updated when I click on the navigation.

How can I achieve the desired functionality in the application? I want the navigation to take us to the desired pages without refreshing the page and that's quite obvious.

Here is the code.

Index.html

<!DOCTYPE html>
<html ng-app="routingApp">
  <head>
    <title>AngularJS routing</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.7/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.7/angular-route.js"></script>
    <script src="script.js"></script>
  </head>
  <body ng-controller="routingAppController">

    <!-- Header and Navigation -->
    <header>
      <nav class= "navbar navbar-default">
        <div class="container">

          <div class="navbar-header">
            <a class="navbar-brand" href="/">Angular Routing App</a>
          </div>

          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
          </ul>

        </div>
      </nav>
    </header>

    <!-- Main Body where content will be injected -->
    <div id="main">
      <div ng-view></div>
    </div>

  </body>
  </html>

Script.js

(function(){
  'use strict';
  var app = angular.module('routingApp', ['ngRoute']);

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

      .when('/', {
        templateUrl : 'pages/home.html',
        controller : 'routingAppController'
      })

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

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

  app.controller('routingAppController', function($scope){
    $scope.message = "This will get updated for sure!";
  });

  app.controller('aboutController', function($scope){
    $scope.message = "This is the about page and it looks awesome!";
  });

  app.controller('contactController', function($scope){
    $scope.message = "This is the contact page!";
  });

})();

One of the pages

<div class="jumbotron text-center">
        <h1>Home Page</h1>

        <p>{{ message }}</p>
    </div>
6
  • what is route you are trying? Commented Feb 22, 2018 at 6:25
  • 1
    Your anchors... they are all #... Commented Feb 22, 2018 at 6:26
  • that mean when you change html, you can't see the changes when you route on it? Commented Feb 22, 2018 at 6:33
  • Yeah, I also did the changes. It is not working then also. Commented Feb 22, 2018 at 6:40
  • Try changing you link like <li><a href="#/about">About</a></li> Commented Feb 22, 2018 at 6:50

0

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.