嵌套的ng-repeat与外部ng-repeat项目影响内部ng-repeat逻辑

时间:2014-04-24 07:00:22

标签: javascript angularjs

我的代码

<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-repeat中的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应该像以前一样。

我如何做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以为相同的内容创建指令,然后使用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

该演示已大大简化,但展示了您可能希望使用的技术。

相关问题