我的对象看起来像这样:
$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>
关于javascript - 如何在 ng-repeat Angularjs 中对对象的内部字段使用 orderBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32110887/