51

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>
Diolor
  • 13,181
  • 30
  • 111
  • 179

2 Answers2

131

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?

Community
  • 1
  • 1
Chandermani
  • 42,589
  • 12
  • 85
  • 88
3

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

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225