I can't stay on the same book when changing chapters

I have problem with the chapter reading, I can’t stay on the same book when changing chapters on specific book.
Instead it does change the whole book for me when changing I want to change to the next chapter…

FYI, it’s the last step on the assignment Reader (Step 9).

This is my ChapterController:

app.controller('ChapterController', ['$scope', 'books', '$routeParams', function($scope, books, $routeParams) {
  books.success(function(data) {
    // Your code here
    $scope.book = data[$routeParams.bookId];
    $scope.chapter = $scope.book.chapters[$routeParams.chapterId];

    // If there no more chapters left, go back to the bookshelf view
    if($routeParams.chapterId >= $scope.book.chapters.length - 1) {
      $scope.nextChapterIndex = "#";

  // Using these properties to create the URLs in line 1 and line 11 of view/chapter.html
  $scope.currentBookIndex = parseInt($routeParams.bookId);
  $scope.currentChapterIndex = parseInt($routeParams.chapterId);
  $scope.nextChapterIndex = $scope.currentChapterIndex + 1;

There’s an issue with the back button though - it doesn’t go back a chapter. I think the original code makes it go straight to the BookShelf view. I’ve modified it so that it goes to the Book view.

I haven’t spent the time trying to make it go back a chapter, and if it is at Chapter 1, then go back to the book view. I guess it would be something like the IF statement that they gave us in the code.

1 Like

I did manage to sort this exercise, I’ll check into your code and see .

Yup the original code makes it go back all the way to the bookshelf view.

To go back a chapter you could do something like:

$scope.previousChapterIndex = $scope.currentChapterIndex - 1;

And then check with an IF statement if $scope.previousChapterIndex is less than 0, you go back to the Book itself :smile:

Anyway this is good practising! Thanks for your advices and help!