I'm having issues with my route file app.js. Whenever I click on any link or button, it redirects me to books.html. What could be the mistake I'm making?
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(['$routeProvider' , function ($routeProvider) {
$routeProvider
.when('/', {
controller: 'BooksController',
templateUrl: '/views/books.html'
})
.when('/books', {
controller: 'BooksController',
templateUrl: '/views/books.html'
})
.when('/books/details/:id', {
controller: 'BooksController',
templateUrl: '/views/books_details.html'
})
.when('/books/add', {
controller: 'BooksController',
templateUrl: '/views/add_book.html'
})
.when('/books/edit/:id', {
controller: 'BooksController',
templateUrl: '/views/edit_book.html'
})
.otherwise({
redirectTo: '/'
})
// $locationProvider.html5Mode(true);
}]);
Every time I click on the view details button in the HTML file, it redirects me back to books.html.
<html>
<div class="panel panel-default" ng-init="getBooks()">
<div class="panel-heading">
<h3 class="panel-title">Latest Books</h3>
</div>
<div class="panel-body">
<div class="row">
<div ng-repeat="book in books">
<div class="col-md-6">
<div class="col-md-6">
<h4>{{book.title}}</h4>
<p>{{book.description}}</p>
<a class="btn btn-primary" href="#/books/details/{{book._id}}">View Details</a>
</div>
<div class="col-md-6">
<img class="thumbnail" ng-src="{{book.image_url}}">
</div>
</div>
</div>
</div>
</div>
</div>
</html>
Could the issue be using the same controller name, BooksController
?
var myApp = angular.module('myApp');
myApp.controller('BooksController', [ '$scope', '$http', '$location', '$routeParams', function ($scope, $http, $location, $routeParams) {
$scope.getBooks = function() {
$http.get('/api/books').then(successCallback, errorCallback);
function successCallback(response){
//success code
$scope.books = response.data;
}
function errorCallback(error){
//error code
$scope.books = error;
}
}
$scope.getBook = function() {
$http.get('/api/books/:id').then(successCallback, errorCallback);
function successCallback(response){
//success code
$scope.book = response.data;
}
function errorCallback(error){
//error code
$scope.books = error;
}
}
}]);
Could there be something wrong with the code in index.html
?
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>BookStore</title>
<script src="https://code.angularjs.org/1.6.0/angular.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.angularjs.org/1.6.0/angular-route.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container" >
<div class="navbar-header">
<button class="navbar-toggler collapsed" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">BookStore</a>
</div>
<div class="collapse navbar-collapse" id="navbar">
<ul class="nav navbar-nav navbar-right">
<li><a href="#/books/add">Add Book</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
<div ng-view>
</div>
</div>
</div>
</div>
<script src="/app.js"></script>
<script src="/controller/books.js"></script>
<script src="/controller/genres.js"></script>
</body>
</html>