当我按 ASC 排序时,我需要在顶部获取空值记录
<tr ng-repeat="footballer in footballers=(footballers | orderBy:predicate)">
predicate : ['team.name','id]
一些足球运动员没有团队,所以团队
object == null
和 team.name==null
,我需要让他们在顶部我想重写排序函数,但我需要保存谓词
最佳答案
你可以在你的 Controller 中使用这样的东西:
$scope.nullsToTop = function(obj) {
return (angular.isDefined(obj.team) ? 0 : -1);
};
在 HTML 上:
<tr ng-repeat="footballer in footballers | orderBy:[nullsToTop].concat(predicate)">
这样你就可以单独维护你的谓词。只需在 orderBy 表达式中连接
nullsToTop
函数以首先运行它。Plunker
关于angularjs - 使用空对象进行 Angular 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25662001/