$ first in ngRepeat

I have an array of $scope.items .

I want the element to be $first one of ngRepeat to add the css in class.

Is this something angular can do? How can I handle boolean values ​​in Ajs?

 <div ng-repeat="item in items"> <div class='' > {{item.title}}</div> </div> 
+47
angularjs angularjs-ng-repeat
Oct 28 '13 at
source share
2 answers

It should be

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

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

+115
Oct 28 '13 at 13:08
source share

You can try the method invocation approach.

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']; 

Fiddle Demo

+4
Oct 28 '13 at 13:40
source share



All Articles