NG-重复指令排序当使用数据(键,值) [英] ng-repeat directive sort the data when using (key, value)

查看:83
本文介绍了NG-重复指令排序当使用数据(键,值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code像这样用NG-重复=(键,值)的数据。
在控制器:

I have a code something like this with ng-repeat = "(key,value) in data". In Controller:

  $scope.Dates = {"Today":"30",
                  "This Week":"42",
                  "This Month": "Oct",
                  "This Quarter" : "Bad",
                  "This Year" : 2013
                                }

和NG-重复指令,因为

and ng-repeat directive as

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

输出进来排序顺序为

The output comes in sorted order as

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

如何摆脱这个排序(怪),因为我想键在code使用的..我检查谷歌组,但有使用两个阵列,其中一个是存储密钥值的小提琴。 http://jsfiddle.net/Saulzar/puhML/3/b 。不想去用这种方法。

How to get rid of this sorting(strange) as I want keys to be used in code.. I checked google group but there was a fiddle for using two arrays of which one was storing the key values. http://jsfiddle.net/Saulzar/puhML/3/b . Don't want to go with this approach.

推荐答案

这是JavaScript的限制没有棱角。

This is limitation of JavaScript not Angular.

从<一个href=\"http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf\">ECMAScript第三版:

4.3.3对象是Object类型的成员。 这是一个无序的集合属性每个都包含一个原始
  值,对象或功能。存储在一个属性的函数
  对象称为方法

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.

语言规范

在[...]为了列举性质[...]
  未指定

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

对象键的明确为了至少提供某种持久的行为。

Angular sorts object keys explicitly in order to provide at least some sort of persistent behavior.

解决方法是遍历提取键:

<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 Week":"42",
  "This Month": "Oct",
  "This Quarter" : "Bad",
  "This Year" : 2013
};

这篇关于NG-重复指令排序当使用数据(键,值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆