Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ng按字母顺序重复索引_Javascript_Angularjs - Fatal编程技术网

Javascript ng按字母顺序重复索引

Javascript ng按字母顺序重复索引,javascript,angularjs,Javascript,Angularjs,我想让$index按字母顺序输出,而不是编号 {{$index}} 这可能吗?您可以简单地使用角度过滤器的orderBy <div ng-repeat="name in names | orderBy:'name'">{{name}}</div> {{name} 以下是我找到的解决方案: Javascript: // the alphabet $scope.alphabet['A','B','C','D','E','F','G','H','I','J','K'

我想让
$index
按字母顺序输出,而不是编号

{{$index}}


这可能吗?

您可以简单地使用角度过滤器的
orderBy

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

以下是我找到的解决方案:

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

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

  return String.fromCharCode(65+index);

};
HTML:

HTML:

{{getLetter($index)}

你可以试试
你是说$index[1,2,3,…]到[a,b,c,…]吗?@BrunoGomes
$index
从0开始向右上升?我想从A开始,然后向上。你为什么不在列表中设置样式:ol{list style type:upper-latin}@brunoGomes因为这不是格式化数据本身的方法,它所做的一切都是设置数据的样式。我根本不想按顺序排序?我只希望索引按字母顺序而不是数字。你能用例子解释一下吗?这样我就可以创建一些适合你的东西了。亚当2。约翰3。Julie你的数据是否在
{'A':'1','B':'2',…}
这种格式没有+I一直在寻找一种适用于所有不同类型数据的解决方案。你可以使用
字符串。fromCharCode(64+索引)
要获得没有字母数组的字符,我将使用
字符串。fromCharCode(65+$index)
因为索引是基于0的,否则,字母以@开头。你可以看到一个样本,我发现这是我的解决方案的失败,太棒了!
// get the index and return the letter of the alphabet
$scope.getLetter = function(index) {

  return String.fromCharCode(65+index);

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