我对Angular很新,我觉得需要的嵌套越多,代码就越复杂。我觉得我在这里错过了一些基本原理。
问题:在chapter.html中我看到了一切正确,但是,点击下一个超链接时,网址会更新,视图不会。
我错过了什么,伙计们?
这是我的代码:
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>
路线
.when('/books/:bookId/chapters/:chapterId', {
controller: 'ChapterController',
templateUrl: 'views/chapter.html'
})