0

我从 mongodb 数据库集合中获得了 Json 数据。

我设法显示了这个 json 的键。

现在,我试图在我的视图中通过双重 ng-repeat 在表格中显示一个数组。

我几乎得到了我想要的东西,但价值顺序不正确。

如果我使用我的项目的属性名称,那很好,但我不希望这样。

我在我的视图中使用此代码:

<table class="table table-striped">
<caption>Content</caption>

<thead>
    <tr>
    <th ng-repeat="caption in captions">
    {{caption}}
    </th>
    </tr>
</thead>

<tbody>
<tr ng-repeat="content in contentCollection">
<td ng-repeat="item in content">
{{item}}
</td>
</tr>
</tbody>
</table> 

将显示:

_id     session     expires

id1         expires1        session1

它应该是:

_id     session     expires

id1     session1    expires1

我用正确的值顺序(id1、session1、expires1)从我的控制器发送一个数组。我可以在带有console.log().

我想我必须做一个指令才能找到解决方法,但也许我错过了一些东西。

你知道什么是错的吗?

谢谢,

只读存储器

4

1 回答 1

0

In an ng-repeat if you want to ensure an order you'll want to use the orderby filter http://docs.angularjs.org/api/ng.filter:orderBy. You should not need a directive but may need a custom comparator for your filter if you need advanced ordering.

If your array is just a list of strings then you'll need a custom filter such as:

angular.module('myApp',[])
    .filter('mySort', function() {
        return function(list) {
            return list.sort();
        }
    });

However given your view I'd assume you have an object and your just pulling the pieces and pre-normalizing for this repeat set. If that's the case you'll want to do something more like the code at http://plnkr.co/edit/ART87TjilotbnsuD02y1?p=preview.

Note: that you'll want to move the array into a service and have the service also return the keys.

Lastly you might consider moving this whole table into a directive to better parse the pieces of your object if your going to be repeating this process.

于 2013-05-27T02:12:57.183 回答