$first in ngRepeat

末鹿安然 提交于 2019-12-03 05:28:28

问题


I have an array $scope.items.

I want if the item is the $first one of the ngRepeat to add the css class in.

Is it something that can be done with angular? Generally how can I handle the booleans in Ajs?

<div ng-repeat="item in items">
   <div class='' > {{item.title}}</div>
</div>

回答1:


It should be

<div ng-repeat="item in items">
   <div ng-class='{in:$first}' > {{item.title}}</div>
</div>

Look at ng-class directive in http://docs.angularjs.org/api/ng.directive:ngClass and in this thread What is the best way to conditionally apply a class?




回答2:


You can try approach with method invocation.

HTML

<div ng-controller = "fessCntrl"> 
<div ng-repeat="item in items">
   <div ng-class='rowClass(item, $index)' > {{item.title}}</div>   
</div>
</div>

JS

var fessmodule = angular.module('myModule', []);

fessmodule.controller('fessCntrl', function ($scope) {
   $scope.items = [
       {title: 'myTitle1', value: 'value1'},
       {title: 'myTitle2', value: 'value2'},
       {title: 'myTitle3', value: 'value1'},
       {title: 'myTitle4', value: 'value2'},
       {title: 'myTitle5', value: 'value1'}
   ];        

     $scope.rowClass = function(item, index){
         if(index == 0){
             return item.value;
         }
        return '';
    };        
});    
fessmodule.$inject = ['$scope'];

Demo Fiddle



来源:https://stackoverflow.com/questions/19635636/first-in-ngrepeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!