嵌套ng重复,外部ng重复项影响内部ng重复逻辑

nested ng-repeat with outer ng-repeat item affecting the inner ng-repeat logic

本文关键字:ng 内部 影响 外部 嵌套 重复      更新时间:2023-09-26

我的代码

<div ng-repeat='n in [] | range:(my_works.items.length/3)+1'>
    <div class="table-row" style="position:absolute;left:13px;{{row_gap_style(n)}}" class='ng-scope'>
         <div class="book_container" ng-repeat='item in my_works.items.slice($index*3,($index*3+3))' style="position:absolute;top:0px;{{col_gap_style($index)}}">
            <div id="" title="{{ item.Story.title}}" class="cover_container fetched {{check_published(item)}}" rel="<?php echo $rel; ?>">

我想做的是

如果外部ng重复中的n为0,那么div.book_container将改为如下所示:

<div class="book_container" ng-show="n == 0" ng-repeat='item in my_works.items.slice($index*2, ($index*2+2))' style="position:absolute;top:0px;{{col_gap_style($index+1)}}">

否则,div应该和以前一样。

我该如何做到这一点?

您可以为相同的内容创建一个指令,然后使用ng-if根据n:的值设置不同的属性

.directive('bookContainer', function() {
  return {
    scope: {
      'items': '=',
      'colstyle': '@'
    },
    restrict: 'E',
    template: '<p ng-repeat="item in items">book: {{item}} {{colstyle}}</p>'
  };
});

视图:

<div ng-repeat='n in []'>
    <book-container ng-if='n==0' items='my_works.slice($index,1)' colstyle='s1'></book-container>
    <book-container ng-if='n!=0' items='my_works.slice($index,2)' colstyle='s2'></book-container>
</div>

Fiddle

这个演示已经大大简化了,但展示了您希望使用的技术。