ng-repeat索引,按字母顺序排列

时间:2022-11-25 16:00:09

I would like to have the $index outputting in an alphabetical format rather than numbered.

我想让$ index以字母格式输出而不是编号。

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

{{$ index}}

Is that possible to do?

这可能吗?

2 个解决方案

#1


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 & @patrick):

编辑(感谢@TheShalit和@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/

#2


You could simply use orderBy filter of angular

你可以简单地使用angularBy过滤器的角度

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

Working Fiddle

#1


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 & @patrick):

编辑(感谢@TheShalit和@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/

#2


You could simply use orderBy filter of angular

你可以简单地使用angularBy过滤器的角度

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

Working Fiddle