Reputation: 71
I'm trying to build my first web-app with MEAN stack and i'm facing an issue with the Angular routing.
<!-- public/index.html -->
<!DOCTYPE HTML>
<html lang="en" content-type="text/css">
<head name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<base href="/">
<title>Trova colori in tinta</title>
<!-- CSS -->
<link rel="stylesheet" href="libs/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css"> <!-- custom styles -->
<link rel="stylesheet" href="normalize.css"> <!-- custom styles -->
<!-- JS -->
<script type="text/javascript" src="libs/angular/angular.min.js"></script>
<script type="text/javascript" src="libs/angular-route/angular-route.min.js"></script>
<!-- ANGULAR CUSTOM -->
<script type="text/javascript" src="js/controllers/MainCtrl.js"></script>
<script type="text/javascript" src="js/controllers/NerdCtrl.js"></script>
<script type="text/javascript" src="js/services/NerdService.js"></script>
<script type="text/javascript" src="js/appRoutes.js"></script>
<script type="text/javascript" src="js/app.js"></script>
</head>
<body >
<div class="container" ng-app="sampleApp" ng-controller="NerdController">
<!-- HEADER -->
<nav class="navbar navbar-default" >
<div class="navbar-header">
<a class="navbar-brand" href="/">Colori</a>
</div>
<!-- LINK TO OUR PAGES. ANGULAR HANDLES THE ROUTING HERE -->
<ul class="nav navbar-nav">
<li><a href="/nerd">Nerds</a></li>
<li><a href="/lacci">lacci</a></li>
</ul>
</nav>
<div class="container-fullwidth"></div>
<!-- ANGULAR DYNAMIC CONTENT -->
<div ng-view></div>
</div>
</body>
</html>
When i click on "Nerds" or "lacci" link it don't show those pages.
here is the file appRoute.js
angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider.when('/', {
templateUrl: '/public/views/home.html',
controller: 'MainController'
}) // nerds page that will use the NerdController
.when('/nerds', {
templateUrl: '/public/views/nerd.html',
controller: 'NerdController'
}).when('/lacci', {
templateUrl: '/public/views/lacci.html',
}).otherwise({ redirectTo: "/home" });
$locationProvider.html5Mode(true);}]);
The two files are in differents folders. Where i'm wrong?
Upvotes: 0
Views: 56
Reputation: 31
In above code you have not mentioned the "ngRoute" dependency. You need to add it inside the blank array of your angular module.
Kindly Run your application using a local server. There is no need for any Tomcat server. You can easily download a Google Chrome Plugin "Web server for Chrome". here- https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb?hl=en
You can also refer to this example here : https://plnkr.co/xRLK9DvPqSHOEtDuEbtZ `
<html ng-app="myApp">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/yeti/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.2/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
<script>
var app = angular.module("myApp", ["ngRoute"]);
app.controller()
app.config(function($routeProvider) {
$routeProvider
.when('/abc', {
'template': "This is the ABC page"
})
.when('/Home', {
'template': "This is the home page"
})
.when('/About', {
'template': "This is the About page"
})
.when('/Contact', {
'template': "This is the Contact page"
})
.when('/Service', {
'template': "This is the Service page"
})
})
</script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Routing App</a>
</div>
<ul class="nav navbar-nav">
<li><a href="#abc">Abc</a></li>
<li><a href="#About">About us</a></li>
<li><a href="#Service">Services</a></li>
<li><a href="#Contact">Contact</a></li>
<li><a href="#Contact">Contact</a></li>
</ul>
</div>
</nav>
</div>
<div ng-view></div>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
</script>
<script src="script.js"></script>
</html>
`
Upvotes: 0
Reputation: 529
I think you forgot to inject 'ngRoute'
angular.module('appRoutes', ['ngRoute']).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider.when('/', {
templateUrl: '/public/views/home.html',
controller: 'MainController'
}) // nerds page that will use the NerdController
.when('/nerds', {
templateUrl: '/public/views/nerd.html',
controller: 'NerdController'
}).when('/lacci', {
templateUrl: '/public/views/lacci.html',
}).otherwise({ redirectTo: "/home" });
$locationProvider.html5Mode(true);}]);
Upvotes: 1