I am just starting to delve into AngukarJS and ionic framework, embarking on the journey of creating an app with a structured tree layout as follows:
Home Tab (list of books) (templates/allbooks.html)
> unique book (book description and chapters) (templates/book-des.html)
>> unique chapter (templates/book-chapter.html)
Here's the content found in books.js
.factory('Books', function() {
// storing books data
var books = [{
id: 0,
title: 'Sample Title',
author: 'Sample Author',
category: 'Horor, Fiction',
cover: '/cover.jpeg',
details: 'some details about the book',
chapters: [
{
id : 1,
name: 'Chapter 1',
filename: 'chapter1.html',
},
{
id : 2,
name: 'Chapter 2',
filename: 'Chapter2.html',
}
]
}
.....
return {
...
// get book by ID
get: function(bookId) {
for (var i = 0; i < books.length; i++) {
if (books[i].id === parseInt(bookId)) {
return books[i];
}
}
return null;
},
// get chapter by ID
getChapter: function(chapterId){
for(var j = 0; j < books.chapters.length; j++){
return j;
}
return null;
}
};
And this is what you'll find in controllers.js
....
.controller('BookDesCtrl', function($scope, $stateParams, Books) {
$scope.book = Books.get($stateParams.bookId);
})
.controller('BookChapterCtrl', function($scope, $stateParams, Books) {
$scope.book = Books.get($stateParams.bookId);
// issue arises when trying to fetch chapter ID
$scope.chapter = Books.getChapter($stateParams.chapterId);
})
.....
In the templates/allbooks.html
, I employed the following ng-repeat
<div ng-repeat="book in books">
<p>{{book.title}}</p>
<p>{{book.author}}</p>
<p>{{book.category}}</p>
</div>
This effectively lists all the available books
In the templates/book-des.html
, another ng-repeat
is used to display the contents as per book:
<div class="book-title">
<p>{{book.title}}</p>
</div>
....
<div ng-repeat="chapter in book.chapters">
<ion-item href="#/tab/books/chapter/{{book.id}}/{{chapter.id}}">
<p>{{chapter.name}}</p>
</ion-item>
</div>
Now, it pulls the current book's title and enlists all its respective chapters. Smooth sailing so far.
However, in the templates/book-chapter.html
, my objective is to spotlight the specific chapter's related contents.
I attempted using
ng-repeat="chapter in book.chapters.id"
but to no avail.
The primary question stands here:
Within the templates/book-chapter.html
template:
<div ng-repeat="chapter in book.chapters.id">
<ion-item class="item item-text-wrap">
<p><div ng-include="'{{chapter.filename}}'"></div></p>
</ion-item>
<p> chapter ID is {{chapter.id}} </p>
</div>
How can I utilize the ng-repeate
feature in that template to extract information pertaining to that specific chapter?