简体   繁体   中英

Angular JS hide first element of ng-repeat

How would I hide the first element of an ng-repeat?

<div ng-repeat="item in items" ng-hide="true">
    <div>{{ item.value }}</div>
</div>

This works in that the whole ng-repeat block is hidden, but how would I hide only the first item in items? I want to display it completely differently using more prominent html/etc, so it's useful to have it in that list of data.

You can do this

<div ng-repeat="item in items" ng-show="!$first">
    <div>{{ item.value }}</div>
</div>

Here are the docs: http://docs.angularjs.org/api/ng.directive:ngRepeat

No need to hide, just use a filter to exclude the first item from the list:

<div ng-repeat="item in items|filter:$index>0">
    <div>{{ item.value }}</div>
</div>

There's a simpler solution in more recent versions of Angular.

The filter "limitTo" now supports a "begin" argument ( docs ):

{{ limitTo_expression | limitTo : limit : begin}}

So you can use it like this in a ng-repeat:

ng-repeat="item in items | limitTo: items.length : 1"

This means that ng-repeat will begin at index 1 (instead of the default index 0) and will continue for the rest of the items array's length (which is less than items.length, but limitTo will handle that just fine).

如果您需要跳过最后一次,请使用ng-if ='!$ last'。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM