我有一个类似这样的代码,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。
原文链接:https://www.f2er.com/angularjs/146406.html4.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 };