遍历包含对象的数组 [英] Iterate over array which includes objects

查看:126
本文介绍了遍历包含对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用ng-repeat查看对象中的不同对象?

I am wondering how can I view different objects within an object using ng-repeat?

到目前为止,这是下面的代码,用于遍历数组中对象的$ ID:

This is the following code that I have so far, to iterate over the $ID of the objects in array:

<div ng-repeat="message in messages"> {{message.$id}} </div>

这是我的数组的样子: http://imgur.com/8quH0iW 如您所见,数组结构如下所示:
--Item [0]:
----- $ Id:xxxxx
----- Message1(唯一ID):
-------------:日期:"XX-XX-XXXX"
-------------:消息:随便什么"
-------------:发件人:"xxxxxxxxxxxxxxxxxx"
----- Message2(唯一ID):
-------------:日期:"XX-XX-XXXX"
-------------:消息:随便什么"
-------------:发件人:"xxxxxxxxxxxxxxxxxx"
--Item [1]:
----- $ Id:xxxxx
----- Message1(唯一ID):
-------------:日期:"XX-XX-XXXX"
-------------:消息:随便什么"
-------------:发件人:"xxxxxxxxxxxxxxxxxx"
----- Message2(唯一ID):
-------------:日期:"XX-XX-XXXX"
-------------:消息:随便什么"
-------------:发件人:"xxxxxxxxxxxxxxxxxx"

This is how my array looks like: http://imgur.com/8quH0iW As you can see, the array structure looks like that:
--Item[0]:
-----$Id:xxxxx
-----Message1(unique ID):
-------------:date: "XX-XX-XXXX"
-------------:message: "whatever"
-------------:sender: "xxxxxxxxxxxxxxxxxx"
-----Message2(unique ID):
-------------:date: "XX-XX-XXXX"
-------------:message: "whatever"
-------------:sender: "xxxxxxxxxxxxxxxxxx"
--Item[1]:
-----$Id:xxxxx
-----Message1(unique ID):
-------------:date: "XX-XX-XXXX"
-------------:message: "whatever"
-------------:sender: "xxxxxxxxxxxxxxxxxx"
-----Message2(unique ID):
-------------:date: "XX-XX-XXXX"
-------------:message: "whatever"
-------------:sender: "xxxxxxxxxxxxxxxxxx"

等等.

TLDR:如何使用ng-repeat对数组项中的对象重复?另外,我可以使用ng-repeat来获取项目中的最后一个对象吗?例如,最近发送的消息?

TLDR: How can I use ng-repeat to repeat over the objects within an item of an array? Also, can I use ng-repeat to get the last object in an item? For example, the last Message sent?

谢谢!

推荐答案

您没有提到数组中的项目是数组还是JSON对象.如果您有JSON对象数组,则可以使用

You don't mention whether the items in your array are arrays or JSON objects. If you have an array of JSON objects then this will work

vm.objectsTest = [{$id : 123}, {$id: 234}];
<ul ng-repeat="o in vm.testObjects"><li ng-bind="o.$id"></li></ul>

如果您的项目是数组,那么您将需要访问数组中的对象,如下所示:

If your items are arrays then you will need to access the object within the array, like this:

vm.objectsTest = [[{$id : 123}], [{$id: 234}]];
<ul ng-repeat="o in vm.testObjects"><li ng-bind="o[0].$id"></li></ul>

或者,如果您需要遍历嵌套数组的项,则可能需要嵌套ng-repeats.但是,最好的选择是更改数据结构,因为过度使用ng-repeat可能会导致性能问题.

Alternately if you need to iterate over the items of your nested array, then you may need to have nested ng-repeats. However the best option would be to change your data structure as using ng-repeat excessively can create performance issues.

请参阅此文章以访问ng-repeat的最后一个元素 ng-repeat中最后一个元素的不同类

See this post for accessing the last element of ng-repeat Different class for the last element in ng-repeat

这篇关于遍历包含对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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