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

标签 javascript angularjs ionic-framework

我是 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/

相关文章:

javascript - 谷歌热图包括点数

javascript - Angularjs 将对象作为属性传递给指令

angularjs - $q.all 比顺序 .then() 慢?

javascript - Highcharts 同时收集 JSON 并通过修改 pointstart 更改日期

Angularjs/Ionic 总是显示小数点后 2 位

javascript - 使用按位运算符(JSFeat)修改图像像素

javascript - 如何在 sails.js/node.js 中实现 Evercookie?

javascript - WordPress javascript 冲突

google-maps - Ionic 2 Segments 不适用于谷歌地图

ionic-framework - ionic : GoogleAnalytics plugin Error: ts: Cannot find name 'GoogleAnalytics'