gpt4 book ai didi

javascript - 如何在 ng-repeat Angularjs 中对对象的内部字段使用 orderBy

转载 作者:行者123 更新时间:2023-11-29 18:04:53 26 4
gpt4 key购买 nike

我的对象看起来像这样:

$scope.options = [{
name: 'John',
phone: '555-1212',
age: 10,
descriptions: [{
"languageId": "EN",
"description": "b Some description",

}, {
"languageId": "DE",
"description": "b Some description in dutch",

}]

},
{
name: 'Jimmy',
phone: '555-1212',
age: 10,
descriptions: [{
"languageId": "EN",
"description": " d Some description",

}, {
"languageId": "DE",
"description": "d Some description in dutch",

}]

},
{
name: 'Cris',
phone: '555-1212',
age: 10,
descriptions: [{
"languageId": "EN",
"description": "a Some description",

}, {
"languageId": "DE",
"description": "a Some description in dutch",

}]

}]

我想根据“描述”中的“描述”字段对其进行排序,

我可以按姓名、电话和年龄等其他字段对其进行排序,但不能按说明。

 <tr ng-repeat="option in options | orderBy:'name':true">
<td> {{option.name}}</td>
<td ng-repeat="desc in option.descriptions |filter:{languageId:'EN'} ">{{desc.description}}</td>


</tr>

请建议我一种按“描述”对数据进行排序的方法。

最佳答案

您可以使用ngInit 预过滤给定语言的描述,然后使用它进行排序和显示:

<tr ng-repeat="option in options | orderBy:'description'" 
ng-init="option.description = (option.descriptions | filter:{languageId:'EN'})[0].description">
<td>{{option.name}}</td>
<td>{{option.description}}</td>
</tr>

演示: http://plnkr.co/edit/eyRYqfeJ6ewaJnGReGTB?p=preview

关于javascript - 如何在 ng-repeat Angularjs 中对对象的内部字段使用 orderBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32110887/

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