使用null对象进行角度排序

rom*_*rus 4 sorting angularjs

当我按ASC排序时,我需要在顶部记录带有空值的记录

<tr ng-repeat="footballer in footballers=(footballers | orderBy:predicate)">
predicate : ['team.name','id]
Run Code Online (Sandbox Code Playgroud)

有些球员没有团队,使团队object == nullteam.name==null,我需要让他们在上面

我想重写sort函数,但我需要保存谓词

bml*_*ite 8

你可以在你的控制器中使用这样的东西:

$scope.nullsToTop = function(obj) {
  return (angular.isDefined(obj.team) ? 0 : -1);
};
Run Code Online (Sandbox Code Playgroud)

在HTML上:

<tr ng-repeat="footballer in footballers | orderBy:[nullsToTop].concat(predicate)">
Run Code Online (Sandbox Code Playgroud)

这样您就可以单独维护谓词.只需nullsToTop将orderBy表达式中的函数连接起来即可先运行它.

Plunker