我是AngukarJS和离子框架的新手,我正在尝试使用以下树结构创建一个应用程序:
Home Tab (list of books) (templates/allbooks.html)
> unique book (book description and chapters) (templates/book-des.html)
>> unique chapter (templates/book-chapter.html)
这是books.js
.factory('Books', function() {
// 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 the book ID
get: function(bookId) {
for (var i = 0; i < books.length; i++) {
if (books[i].id === parseInt(bookId)) {
return books[i];
}
}
return null;
},
// get the chapter ID
getChapter: function(chapterId){
for(var j = 0; j < books.chapters.length; j++){
return j;
}
return null;
}
};
使用以下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);
// this returns error for getting chapter ID
$scope.chapter = Books.getChapter($stateParams.chapterId);
})
.....
templates/allbooks.html
中的我有以下ng-repeat
<div ng-repeat="book in books">
<p>{{book.title}}</p>
<p>{{book.author}}</p>
<p>{{book.category}}</p>
</div>
成功列出所有书籍
templates/book-des.html
中的我有以下ng-repeat
<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>
获取“当前”书名并列出所有章节。 到目前为止一切都很好
但在templates/book-chapter.html
我希望能够显示与该特定章节相关的内容。
我尝试使用ng-repeat="chapter in book.chapters.id"
,但它无效。
主要问题在于:
<{1>}模板中的:
templates/book-chapter.html
如何使用该模板中的<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>
来提取与该独特章节相关的信息?
答案 0 :(得分:0)
模板更新
<ion-item class="item item-text-wrap">
<p><div ng-include="'{{chapter.filename}}'"></div></p>
</ion-item>
<p> chapter ID is {{chapter.id}} </p>
更新您的控制器 -
controller('BookChapterCtrl', function($scope, $stateParams, Books) {
var book = Books.get($stateParams.bookId);
$scope.book = book;
// this returns error for getting chapter ID
$scope.chapter = Books.getChapter(book, $stateParams.chapterId);
})
更新您的app.js
getChapter: function(book, chapterId) {
for (var i = 0; i < book.chapters.length; i++) {
if (book.chapters[i].id === parseInt(chapterId)) {
return book.chapters[i];
}
}
return null;
}