gpt4 book ai didi

javascript - Angularjs - 简单的 ng-repeat orderBy 过滤器不起作用

转载 作者:行者123 更新时间:2023-11-29 18:12:19 25 4
gpt4 key购买 nike

我有一个简单的对象数组:

  $scope.asd = [{"approved":true,"id":"542fb8972be5b5cc4356dd51","username":"k"},
{"approved":false,"id":"542fbea3bae6fe4449c838d5","username":"tototod"},
{"approved":false,"id":"54324929afafdb92209b2474","username":"asdasd"}
];

然后在 View 中我想像这样打印值和 orderBy id 或用户名:

<div ng-repeat="row in asd | orderBy:username:reverse">
{{row.username}}
</div>

<div ng-repeat="row in asd | orderBy:id:reverse">
{{row.id}}
</div>

但它不起作用,它可以是什么?

我没有控制台错误,我只是按照这里的指南操作:https://docs.angularjs.org/api/ng/filter/orderBy

任何帮助appriciated,谢谢

最佳答案

在您的示例中,usernameid 应该被引用!该表达式必须求值为一个字符串,该字符串将用于查找对象属性。请再次引用文档,您将看到 predicate 是一个解析为字符串值的变量。

解决方法:

<div ng-repeat="row in asd | orderBy:'username':true">
{{row.username}}
</div>

此外,reverse 的计算结果应为 bool 表达式。

工作 fiddle为您的数据。

关于javascript - Angularjs - 简单的 ng-repeat orderBy 过滤器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26214232/

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