Ng-repeat index in alphabetical format

I would like to have the output $index

in alphabetical format, not numbered.

<div ng-repeat="name in names">{{$index}}</div>

Can this be done?

+3


source to share


2 answers


Here is the solution I found:

JavaScript:

// the alphabet    
$scope.alphabet['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

// get the index and return the letter of the alphabet
$scope.getLetter = function(index) {

  return $scope.alphabet[index];

};

      

HTML:

<div ng-repeat="name in names">{{getLetter($index)}}</div>

      

Edit (thanks to @TheShalit and @patrick):



JavaScript:

// get the index and return the letter of the alphabet
$scope.getLetter = function(index) {

  return String.fromCharCode(65+index);

};

      

HTML:

<div ng-repeat="name in names">{{getLetter($index)}}</div>

      

http://jsfiddle.net/cLto7mff/

+5


source


You can just use orderBy

angular filter

<div ng-repeat="name in names | orderBy:'name'">{{name}}</div>

      



Working script

+1


source







All Articles