另一个数组里面的Javascript数组

时间:2015-06-11 03:58:33

标签: javascript arrays loops ionic-framework

我从离子框架开始,我被困在创建并调用另一个数组中的数组。

我的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',
    // my first attempt to create an array to store chapters info
    chapters: [
      {
        name: 'Chapter 1',
        filename: 'chapter1.html',
      },
      {
        name: 'Chapter 2',
        filename: 'Chapter2.html',
      }
    ]
  }, {
    id: 1,
    title: 'Sample Title Two',
    author: 'Sample Author two',
    category: 'Horor, Fiction',
    cover: '/cover.jpeg',
    details: 'some details about the book',
    // my first attempt to create an array to store chapters info
    chapters: [
      {

        name: 'Chapter 1',
        filename: 'chapter1.html',
      },
      {
        name: 'Chapter 2',
        filename: 'Chapter2.html',
      }
    ]
  },

访问数据时,我使用{{book.title}}并输出书名。但我不知道如何返回章节值,因为每本书都有不同的章节数。

也许为每章分配章节ID并调用该ID?

1 个答案:

答案 0 :(得分:0)

尝试将其显示在Angular上

<div ng-repeat="book in books">
    {{book.title}}
    <div ng-repeat="chapter in book.chapters">
        {{chapter.name}}
    </div>
</div>