Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm quite new to Angular, and I feel that the more nesting there needs to be, the more complex the code. I feel that I'm missing some fundamentals here.

Problem: In the chapter.html I see everything correctly, however, when clicking the next hyperlink, the URL gets updated, the view does not.

What am I missing, guys?

Here's my code:

ChapterController.js

app.controller('ChapterController', ['$scope', 'books', '$routeParams', function($scope, books, $routeParams) {
  books.success(function(data) {

    // Your code here
    $scope.book = data[$routeParams.bookId];
    $scope.chapter = data[$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;

}]);

chapter.html

<a class="button back" href="#/books/{{ currentBook }}">Back</a>
<div class="chapter" ng-repeat="chapter in book.chapters">
  <p><span class="title"> {{book.title}} </span> <span class="author"> {{book.author}} </span></p>
  <h2 class="chapter-title"> {{chapter.title}} </h2>

  <p ng-repeat="paragraph in chapter.paragraphs">{{ paragraph }}</p>

  <a class="button next" href="#/books/{{ currentBookIndex }}/chapters/{{ nextChapterIndex }}">Next</a>
</div>

Route

.when('/books/:bookId/chapters/:chapterId', {
    controller: 'ChapterController',
    templateUrl: 'views/chapter.html'
  })
share|improve this question
    
if you check the network out does the browser say its grabbing chapter.html or is there an error or anything? – TimCodes 15 hours ago
    
@TimCodes There seems no error. The URLs chapterId gets updated and the controllers IF block fires after a few clicks (when the paragraphs array ends) – Roland Jegorov 15 hours ago
    
log $scope.book and $scope.chapter to console and see if they are loaded when the controller loads – TimCodes 15 hours ago
    
@TimCodes I'm working in Codecademy. It does use a server simulator browser in the exercises. Haha, I just scrolled the chapter.html and noticed that all the paragraphs are printed one after another already. – Roland Jegorov 15 hours ago

2 Answers 2

Please check is your this route is the final one

because of :bookId you need to put this when at the end, typically just before otherwise

.when('/books/:bookId/chapters/:chapterId', {
    controller: 'ChapterController',
    templateUrl: 'views/chapter.html'
  })
share|improve this answer

use ng-href directive.

refer : https://docs.angularjs.org/api/ng/directive/ngHref

<a class="button next" ng-href="#/books/{{ currentBookIndex }}/chapters/{{ nextChapterIndex }}">Next</a>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.