保持插入顺序角 [英] Keeping insertion order angular

查看:136
本文介绍了保持插入顺序角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有过关键迭代,值集合的HTML片段。当我创建一个对象,并放了进去,然后循环槽通过HTML片段的对象一定的价值,都完美的作品。

I have a HTML fragment that iterates over key, value collection. When I create an object and put some value in, then iterate trough that object via HTML fragment, all works perfectly.

不过,因为我需要在特定的顺序按键,我米使用的地图而不是简单的对象。当我调试,我可以看到我的插入顺序是preserved,但此时由于某种原因,HTML片段,这似乎在集合迭代不知道怎么做。我看到我的屏幕上也没有,当我使用的地图对象,相对于常规对象,当我看到无序含量

However since I need keys in specific order, I m using a Map instead of plain object. This time when I debug I can see that my insertion order was preserved, but for some reason the HTML fragment which iterates over collection doesn't seem to know how to do so. I see nothing on my screen when I use the map object, opposed to the regular object when I see unordered content

tr ng-repeat="(key, value) in rowTitlesValues"

是我的HTML片段的样子,当我切换rowTitlesValues​​回对象再次工作,我在做什么错了,一个人如何保持插入顺序或者我怎么样反对所以它的键定制订单?

Is how my HTML fragment looks like, when I switch rowTitlesValues back to object works again, what am I doing wrong, and how does one keep insertion order or how do I sort object so it's keys are in custom order?

推荐答案

从角参考(链接):

有可能得到ngRepeat遍历使用以下语法的对象的属性:

Iterating over object properties

It is possible to get ngRepeat to iterate over the properties of an object using the following syntax:

< D​​IV NG重复=(键,值)MyObj中> ...< / DIV>

您需要注意的是,JavaScript规范不定义键返回一个对象的顺序。 (要在1.3角使用的按键按字母顺序排序的ngRepeat指令缓解这一点。)

You need to be aware that the JavaScript specification does not define the order of keys returned for an object. (To mitigate this in Angular 1.3 the ngRepeat directive used to sort the keys alphabetically.)

1.4版去掉字母排序。我们现在赖以MyObj中的关键运行时,由浏览器返回的顺序。似乎浏览器通常按照它们被定义的顺序提供密钥,战略[...]

Version 1.4 removed the alphabetic sorting. We now rely on the order returned by the browser when running for key in myObj. It seems that browsers generally follow the strategy of providing keys in the order in which they were defined, [...]

如果这不是想要的,建议的解决方法是将您的对象转换成排进你将其提供给ngRepeat前preFER顺序的数组。你就可以像toArrayFilter这种滤波器做到这一点还是实现了自己的目标在$手表。

If this is not desired, the recommended workaround is to convert your object into an array that is sorted into the order that you prefer before providing it to ngRepeat. You could do this with a filter such as toArrayFilter or implement a $watch on the object yourself.

此外,我不认为角的1.x知道如何遍历地图。我相信行在code证明了这一点:

Additionally, I do not think Angular 1.x knows how to iterate over a Map. I believe this line in the code proves it:

collectionKeys = [];
for (var itemKey in collection) { // iterates your object using `in`, not `of` or `Map.forEach()`
    ...
}
// ng-repeat then iterates the collectionKeys to create the DOM

所以,你可能需要为行动角文档提示:

So you will probably need to act as Angular docs suggest:

[...] 转换你的对象成排进你将其提供给ngRepeat前preFER顺序的数组。你就可以像toArrayFilter这种滤波器做到这一点还是实现了自己的目标在$手表。

这篇关于保持插入顺序角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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