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

<ahref="/">Main</a>

Solution 2:

enter code here

<!DOCTYPE html><html><scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-route.min.js"></script><bodyng-app="myApp"><p><ahref="#/">Main</a></p><ahref="#/red">Red</a><ahref="#/green">Green</a><ahref="#/blue">Blue</a><divng-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><ahref="/">Main</a></p><ahref="/red">Red</a><ahref="/green">Green</a><ahref="/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?"