在 Angular JS 中跳过 ng-repeat JSON 排序 [英] Skip ng-repeat JSON ordering in Angular JS

查看:27
本文介绍了在 Angular JS 中跳过 ng-repeat JSON 排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道我如何在使用 ng-repeat 时完全跳过 JSON 排序(可能是一种轻松的方式)?

例如,我的源 JSON 看起来像这样 -

<代码>{"title": "标题","描述": "描述","moreInfo": "更多信息"}

一旦我在 ng-repeat 中使用它,它就会按字母顺序排列它们.像这样 -

<代码>{"描述": "描述","moreInfo": "更多信息","title": "标题"}

我的 ng-repeat 看起来像这样 -

<span class="one">{{key}} </span><span class="two">{{数据}} </span>

我见过有人使用单独的键数组并使用它们来识别 JSON 对象,最终避免了按字母顺序排序.

有没有一种优雅的方法来做到这一点?

解决方案

在 google 群组中找到了不错的解决方法:

 

<预>键:{{键}}价值:{{价值}}

在范围内:

 $scope.data = {'key4':'数据4','key1':'数据1','key3':'数据3','key2':'数据2','key5':'数据5'};$scope.notSorted = function(obj){如果(!对象){返回 [];}返回 Object.keys(obj);}

工作:http://jsfiddle.net/DnEXC/

原文:https://groups.google.com/forum/#!topic/angular/N87uqMfwcTs

Does anybody know how I can SKIP JSON ordering altogether when I use ng-repeat (in a painless way probably)?

For example, my source JSON looks something like this -

{
   "title": "Title",
   "description": "Description",
   "moreInfo": "Moreinformation"
}

Once I use it in ng-repeat, it orders them alphabetically. Something like this -

{
   "description": "Description",
   "moreInfo": "Moreinformation",
   "title": "Title"
}

My ng-repeat looks something like this -

<div ng-repeat="(key,data) in items">
   <span class="one"> {{key}} </span>
   <span class="two"> {{data}} </span>
</div>

I've seen people having a separate array of the keys and using them to identify the JSON objects, ultimately avoiding alphabetical sorting.

Is there an elegant way to do this?

解决方案

Nice workaround found at google groups:

    <div ng-repeat="key in notSorted(data)" ng-init="value = data[key]">
         <pre>
               key: {{key}}
               value: {{value}}
         </pre>           
    </div>

And in scope:

    $scope.data = {
        'key4': 'data4',
        'key1': 'data1',
        'key3': 'data3',
        'key2': 'data2',
        'key5': 'data5'
    };

    $scope.notSorted = function(obj){
        if (!obj) {
            return [];
        }
        return Object.keys(obj);
    }

Working: http://jsfiddle.net/DnEXC/

Original: https://groups.google.com/forum/#!topic/angular/N87uqMfwcTs

这篇关于在 Angular JS 中跳过 ng-repeat JSON 排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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