Reputation: 327
I was working with routing in angularjs
My aim here is to use routing to route pages to the index.html page.
When I click the home and courses anchor tag I expect to include the contents of the respected files home.html
and courses.html
to load in ng-view
which is showing blank when I click.
There is also no error in the console which makes me hard to understand.
I am a beginner in angularjs.
Please comment for any query.
Below is the code for index.html
<!DOCTYPE html>
<html>
<head>
</style>
</head>
<body ng-app="myModule">
<div>
<h1>Indexpage</h1>
<ul>
<li>
<a href="#/home">Home</a>
</li>
<li>
<a href="#/courses">Courses</a>
</li>
</ul>
</div>
<div>
<ng-view></ng-view>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
<script src="custom.js"></script>
</body>
</html>
Below is the code for home.html
<h1>
{{ message }}
</h1>
<div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
Below id the code for courses.html
<h1>
{{ message }}
</h1>
<div>
<ul>
<li ng-repeat="course in courses">
{{ course }}
</li>
</ul>
</div>
Below is the code for custom.js
var myApp = angular.module("myModule",["ngRoute"])
.config(function($routeProvider){
$routeProvider
.when("/home",{
templateURL : "Templates/home.html",
controller : "homeController"
})
.when("/courses",{
templateURL : "Templates/courses.html",
controller : "courseController"
});
})
.controller("homeController",function($scope){
$scope.message = "Home";
})
.controller("courseController",function($scope){
$scope.message = "Courses";
$scope.courses = ["PHP","JAVA","mysql","Oracle","javascript"];
}));
Upvotes: 1
Views: 293
Reputation: 692211
I made a plnkr, which made several things obvious:
</style>
There are two reasons, as shown in the plnkr:
#!/home
and #!/courses
, not #/home
and #/courses
(see the documentation)templateUrl
, not templateURL
(see the documentation).Upvotes: 3