如何以ng重复为中心最后一项

时间:2018-05-15 09:21:23

标签: html css angularjs

我正在使用ng-repeat来创建元素 我使用过:nth-child(4n+1)一行只显示4个元素,但我希望最后一个div元素居中对齐。
有没有办法在Angular中实现这一点? 假设有9个元素,我想在中心显示第9个元素。

HTML:

<div class="container " ng-app="myApp" ng-controller="myCtrl">
  <div class="row LaunchTrackerBrands text-center" >
    <div class="col-md-2 col-sm-4 Brandtracker" ng-repeat="data in LauncherData">
      <div class="BrandName " title="{{data.Brand}}" >
        <img src={{data.src}} draggable="false" text-center/>
         <div class="Metaname">
            <div class="inner-triangle" style="border-left: {{data.color}} 30px solid"></div>
            {{data.name}}
          </div>
        </div>
      </div>
    </div>
  </div>

CSS

body {
    background-color: rgb(247, 247, 247);
}

.LaunchTrackerBrands {
    margin-top: 5em;
}

.Brandtracker {
    margin-left: 2em
}

.LaunchTrackerBrands :nth-child(4n+1) {
    clear: both;
}

.BrandName {
    background-color: white;
    border-radius: 10px;
    box-shadow: 2px 2px 2px #999;
    margin: 20px;
    margin-left: 2em;
    width: 150px;
    margin-left: 2em;
}

    .BrandName a img {
        padding: 0.5em;
        width: auto;
        height: auto;
        max-width: 100%;
    }

.Metaname {
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
    background-color: #f8f8f8;
    padding: 4px;
    position: relative;
    overflow: hidden;
}

.inner-triangle {
    border-right: 25px solid transparent;
    border-bottom: 25px solid transparent;
    position: absolute;
    left: 0px;
    top: 0px;
    z-index: 4;
}

2 个答案:

答案 0 :(得分:4)

试试这个,

<div class="col-md-2 col-sm-4 Brandtracker"
     ng-repeat="data in LauncherData"
     ng-class="{'alignCenter':$last}">
</div>

其中alignCenter是将元素与中心对齐的类。

答案 1 :(得分:0)

因此,您可以执行的操作如下:使用index保存ng-repeat中的当前$index。如果当前索引与循环列表的ng-class相同,则使用length来检查,因此它是每种情况下的最后一项。如果条件为真,则添加一个以元素为中心的class

angular.module("myApp", []).controller("myController", function($scope) {
  $scope.myList = [{
      id: 0,
      text: "item 1"
    },
    {
      id: 1,
      text: "item 2"
    },
    {
      id: 2,
      text: "item 3 (last one)"
    }
  ];

  $scope.myList2 = [{
      id: 0,
      text: "item 1"
    },
    {
      id: 1,
      text: "item 2"
    },
    {
      id: 2,
      text: "item 3"
    },
    {
      id: 3,
      text: "item 4"
    },
    {
      id: 4,
      text: "item 5 (last one)"
    }
  ];
});
.list {
  display: flex;
  flex-direction: column;
}

.list--centered {
  align-items: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myController" class="wrapper">
  <div class="list" ng-repeat="item in myList" ng-init="idx = $index" ng-class="myList.length == idx + 1 ? 'list--centered' : ''">
    <span class="list__item">{{item.text}}{{idx}}</span>
  </div>
  <br><br><br>
  <div class="list" ng-repeat="item in myList2" ng-init="idx = $index" ng-class="myList2.length == idx + 1 ? 'list--centered' : ''">
    <span class="list__item">{{item.text}}{{idx}}</span>
  </div>
</div>