gpt4 book ai didi

javascript - 我如何在使用 ng-repeat 循环访问相同的 json 时按 json 的属性之一进行排序?

转载 作者:行者123 更新时间:2023-11-28 05:39:04 27 4
gpt4 key购买 nike

html:-

<div class="question" ng-repeat="x in asdf">
<p >{{x}}</p>
</div>

json:-

  $scope.asdf = {
"1": {"question":"Jquery, hide radio button label if not selected","votes": "233", "answers": "2", "views": "2", "tags": "html", "dateString": "Aug 22 2016 16:52", "date": "1471864975000"},
"2": {"question":"Jquery, hide radio button label if not selected","votes": "233", "answers": "2", "views": "2", "tags": "html", "dateString": "Aug 22 2016 16:52", "date": "1471864975000"},
"3": {"question":"Jquery, hide radio button label if not selected","votes": "233", "answers": "2", "views": "2", "tags": "html", "dateString": "Aug 22 2016 16:52", "date": "1471864975000"},
"5": {"question":"Jquery, hide radio button label if not selected","votes": "233", "answers": "2", "views": "2", "tags": "html", "dateString": "Aug 22 2016 16:52", "date": "1471864975000"},
"6": {"question":"Jquery, hide radio button label if not selected","votes": "233", "answers": "2", "views": "2", "tags": "html", "dateString": "Aug 22 2016 16:52", "date": "1471864975000"},
"7": {"question":"Jquery, hide radio button label if not selected","votes": "233", "answers": "2", "views": "2", "tags": "html", "dateString": "Aug 22 2016 16:52", "date": "1471864975000"},
"8": {"question":"Jquery, hide radio button label if not selected","votes": "233", "answers": "2", "views": "2", "tags": "html", "dateString": "Aug 22 2016 16:52", "date": "1471864975000"},
"9": {"question":"Jquery, hide radio button label if not selected","votes": "233", "answers": "2", "views": "2", "tags": "html", "dateString": "Aug 22 2016 16:52", "date": "1471864975000"}
};

这就是我想做的,按日期排序(日期是 json 中的属性)

<div class="question" ng-repeat="x in asdf | orderBy : date">
<p >{{x}}</p>
</div>

也尝试过

<div class="question" ng-repeat="x in asdf | orderBy : x.date">
<div class="question" ng-repeat="x in asdf | orderBy : asdf.date">

似乎没什么作用

1 - 为什么这是不可能的?

2 - 我应该怎样做才能获得类似的结果?

注意 - 我想在特定的 div 上重复使用 class="question",不想创建另一个 div。

最佳答案

在本例中,orderBy 过滤器参数必须是字符串。所以这应该有效:

<div class="question" ng-repeat="x in asdf | orderBy : 'date'">
<p>{{x}}</p>
</div>

这是文档的解释:

string: An Angular expression. This expression will be evaluated against each item and the result will be used for sorting. For example, use 'label' to sort by a property called label or 'label.substring(0, 3)' to sort by the first 3 characters of the label property. (The result of a constant expression is interpreted as a property name to be used for comparison. For example, use '"special name"' (note the extra pair of quotes) to sort by a property called special name.)

来自https://docs.angularjs.org/api/ng/filter/orderBy

编辑:但是 orderBy 过滤器仅适用于数组(或类似数组的对象)

The collection can be an Array or array-like object (e.g. NodeList, jQuery object, TypedArray, String, etc).

因此,您首先需要将数据放入不同的结构中,或者使用自定义解决方案对其进行排序。例如:

Angular orderBy object possible?

关于javascript - 我如何在使用 ng-repeat 循环访问相同的 json 时按 json 的属性之一进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39080414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com