json3 json3 - 4 months ago 24
AngularJS Question

Why isn't my route loading?

I am new to angularjs and for some reason my routes isnt working, when I click on the test link. I have rechecked my code over and over and cant seem to find the problem.

Any help would be really appreciated!

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>


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



<style>


</style>

</head>


<body>


<a href="#test">Test</a>

<div ng-view></div>


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

</body>
</html>

Answer

There are couple of things I wanted to point out which you have missed.

  1. You missed to kick off angular on page. You could do it by doing ng-app="myApp" as myApp module you already defined.
  2. Other thing is, you have to change your href to href="#/test", so that on click of link you will navigate to /test route