AngularJS First in array after Filter

前端 未结 3 1757
时光取名叫无心
时光取名叫无心 2021-01-07 23:34

In my controller I can call:

$scope.games[0];

To access the first item in my games array. Is there a way to do this keeping Filters in mind

3条回答
  •  余生分开走
    2021-01-08 00:17

    Not with bracket access. If you have an ng-repeat with a filter:

    ng-repeat="thing in things | filter:search"
    

    The filtered list here is kind of anonymous - it doesn't have a name that you can access.

    That said, if you take a look at the docs for ngRepeat, you'll see that inside each repeater's scope, you have access to $index, $first, $middle, and $last.

    So something like

    
        
            n={{n}}:index={{$index}}:first={{$first}}:middle{{$middle}}:last={{$last}}
        

    Would yield:

        n=1:index=0:first=true:middlefalse:last=false
    
        n=3:index=1:first=false:middletrue:last=false
    
        n=5:index=2:first=false:middlefalse:last=true
    

    Fiddle

提交回复
热议问题