angular-js将数组表示为2d表

时间:2015-01-10 07:12:23

标签: html angularjs ng-repeat

我的模型中有一系列书籍:

(function() {
  var app = angular.module("googleBooksViewer", []);
  var mainController = function($scope, $http) {
    $scope.search = function(bookname) {
      $http.get("https://www.googleapis.com/books/v1/volumes?q=" + bookname)
        .then(function(response) {
          $scope.books = response.data;
        });
    };
  };
  app.controller("mainController", mainController);
})();

我知道我可以使用ng-repeat将图书缩略图作为列表呈现,但我希望它用每行8张缩略图填充屏幕。 有没有办法在我的视图html中执行此操作,或者我必须在我的模型中创建二维数组吗?

1 个答案:

答案 0 :(得分:1)

我想说这更多是CSS问题。您可以对book块使用%width,使其每行占用8个块(12.5%宽度)。然后使用ngRepeat的HTML非常简单:

<div class="container">
    <div class="book" ng-repeat="book in books">
        {{book.title}} ...
    </div>
</div>

主要工作由CSS完成:

.container .book {
    width: 12.5%;
    float: left;
    /* ... */
}

查看下面的演示。

&#13;
&#13;
.container {
    overflow: hidden;
    border: 1px #DDD solid;
}
.container .book {
    width: 12.5%;
    float: left;
    padding: 5px;
    background: #EEE;
    box-sizing: border-box;
    border: 1px #AAA solid;
}
&#13;
<div class="container">
    <div class="book">Book title 1</div>
    <div class="book">Book title 2</div>
    <div class="book">Book title 3</div>
    <div class="book">Book title 4</div>
    <div class="book">Book title 5</div>
    <div class="book">Book title 6</div>
    <div class="book">Book title 7</div>
    <div class="book">Book title 8</div>
    <div class="book">Book title 9</div>
    <div class="book">Book title 10</div>
    <div class="book">Book title 11</div>
    <div class="book">Book title 12</div>
    <div class="book">Book title 13</div>
    <div class="book">Book title 14</div>
    <div class="book">Book title 15</div>
    <div class="book">Book title 16</div>
    <div class="book">Book title 17</div>
    <div class="book">Book title 18</div>
    <div class="book">Book title 19</div>
    <div class="book">Book title 20</div>
    <div class="book">Book title 21</div>
    <div class="book">Book title 22</div>
    <div class="book">Book title 23</div>
    <div class="book">Book title 24</div>
</div>
&#13;
&#13;
&#13;