angularjs – ng-repeat指令在使用(key,value)时对数据进行排序

前端之家收集整理的这篇文章主要介绍了angularjs – ng-repeat指令在使用(key,value)时对数据进行排序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个类似这样的代码,ng-repeat =“(key,value)in data”。
在控制器:
$scope.Dates = {"Today":"30","This Week":"42","This Month": "Oct","This Quarter" : "Bad","This Year" : 2013
                                }

和ng-repeat指令

<div ng-repeat="(key,value) in Dates">
{{key}} ==> {{value}}
</div>

输出按照排序顺序

This Month ==> Oct
This Quarter ==> Bad
This Week ==> 42 
This Year ==> 2013
Today ==> 30

如何摆脱这种排序(奇怪),因为我想要在代码中使用的键。我检查了google组,但有一个小提琴使用两个数组,其中之一是存储的键值。 http://jsfiddle.net/Saulzar/puhML/3/b。不想用这种方法去。

这是JavaScript的限制,而不是Angular。

ECMAScript Third Edition

4.3.3 An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive
value,object,or function. A function stored in a property of an
object is called a method.

ECMAScript Language Specification

The […] order of enumerating the properties […]
is not specified.

Angular sorts object keys explicitly为了提供至少某种持久的行为。

解决方法是对提取的键进行迭代:

<div ng-repeat="key in keys(Dates)">
  {{key}} ==> {{Dates[key]}}
</div>
$scope.keys = function(obj){
  return obj? Object.keys(obj) : [];
}

$scope.Dates = {
  "Today":"30","This Year" : 2013
};
原文链接:https://www.f2er.com/angularjs/146406.html

猜你在找的Angularjs相关文章