Skip to content Skip to sidebar Skip to footer

Why Angular Js Routing Is Not Working For This Example?

I am learning routing in angular js, please help me with following example. Routing is not working in this example. Do I need to run this on any server?

Solution 1:

Remove the # from the href and try once

<a href="/">Main</a>

Solution 2:

enter code here

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-route.min.js"></script>

<body ng-app="myApp">

  <p><a href="#/">Main</a>
  </p>

  <a href="#/red">Red</a>
  <a href="#/green">Green</a>
  <a href="#/blue">Blue</a>

  <div ng-view></div>

  <script>
    var app = angular.module("myApp", ["ngRoute"]);
    app.config(function($routeProvider) {
      $routeProvider
        .when("/", {
          templateUrl: "main.html"
        })
        .when("/red", {
          templateUrl: "red.html"
        })
        .when("/green", {
          templateUrl: "green.html"
        })
        .when("/blue", {
          templateUrl: "blue.html"
        });
    });
  </script>

  <p>Click on the links to navigate to "red.htm", "green.htm", "blue.htm", or back to "main.htm"</p>
</body>

</html>

Issue is with your angular-route cdn .


Solution 3:

Try URL as

<p><a href="/">Main</a></p>

<a href="/red">Red</a>
<a href="/green">Green</a>
<a href="/blue">Blue</a>

Then only your route prams set with your URL


Post a Comment for "Why Angular Js Routing Is Not Working For This Example?"