加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 08:58:22 所属栏目:安全 来源:网络整理
导读:我有一个类似这样的代码,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
我有一个类似这样的代码,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
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读