簡體   English   中英

另一個數組內的Javascript數組

[英]Javascript array inside another array

我從離子框架開始,我被困在另一個數組中創建和調用一個數組。

我的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?

嘗試在Angular上顯示它

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM