如何在ng-repeat中显示元素一次

mat*_*iel 6 angularjs

我需要按价格循环查看列表顺序,一旦价格不存在,我就会显示一条不可用的消息,但我不想为每个空元素显示它.我正在使用角度1.2

<div ng-repeat="item in list | orderBy: 'cost'">

  <div ng-if="cost == 0 and not already shown">Sorry the following are unavailable</div>
  <div>...my item here...</div>
<div>
Run Code Online (Sandbox Code Playgroud)

mat*_*iel 1

这是我能找到的完成它的最佳方法。

标记

<div class="sold-out-message" ng-if="displaySoldOutMessage(item)">Sorry, sold out</div>
Run Code Online (Sandbox Code Playgroud)

控制器

$scope.firstSoldOutItemId = false;
$scope.displaySoldOutMessage = function(item) {
   if ( item.cost ) return false;

   $scope.firstSoldOutItemId = $scope.firstSoldOutItemId || item.id;

   return item.id == $scope.firstSoldOutItemId;
};
Run Code Online (Sandbox Code Playgroud)