gpt4 book ai didi

javascript - Angularjs ng-repeat 每个唯一的 ID

转载 作者:行者123 更新时间:2023-11-28 07:16:28 28 4
gpt4 key购买 nike

我是 AngukarJS 和 ionic 框架的新手,我正在尝试创建一个具有以下树结构的应用程序:

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" 但它不起作用。

主要问题在这里:

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>

如何使用该模板中的 ng-repeate 来提取与该独特章节相关的信息?

最佳答案

模板更新

  <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 -.

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;

}

关于javascript - Angularjs ng-repeat 每个唯一的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30790966/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com