如何在NG重复中显示一次元素



我需要按价格循环循环订单,一旦价格不在。我正在使用Angular 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>

您可以有条件地显示两个跨度 - 一个是0(您的'不可用'消息),另一个是其他任何东西。

<ul>
  <li ng-repeat="d in newData track by $index">
    <span ng-show="d > 0">{{d}}</span>
    <span ng-show="d === 0">Not Available</span>
  </li>
</ul>

数据可以通过函数传递以在第一个函数上拉出所有0

  $scope.data = [1,2,3,0,1,0,0,1,0,2]
  $scope.pullDupes = function(array) {
    var newArray = [];
    var zero;
    for(var i = 0; i < array.length; i++) {
      if (array[i] !== 0) {
        newArray.push(array[i])
      }
      if (array[i] === 0 && !zero) {
        zero = true;
        newArray.push(array[i])
      } 
    }
    return newArray;
  }
  $scope.newData = $scope.pullDupes($scope.data);

plunker

您只能显示第一个消息,请参阅此处:

<div ng-repeat="item in list | orderBy: 'cost'">
    <div style="color:red" ng-show="item.cost == 0 && $first">Message Goes Here</div>
    <hr>
    <div>{{item.name}} - Price : {{item.cost}}</div>
</div>

这是它的一个plunker:

http://plnkr.co/edit/rwzpzp9rfichwxqf71o7?p=preview

您也使用错误的ng-if,您需要像下次

那样这样做像这样的 item.cost

欢呼!

这是我能找到的最好的方法。

标记

<div class="sold-out-message" ng-if="displaySoldOutMessage(item)">Sorry, sold out</div>

控制器

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

您可以尝试使用$范围。

   <div ng-model="actualItem" ng-repeat="item in list | orderBy: 'cost'">
             <div ng-if="cost == 0 && oneMessage == true">Sorry the following are unavailable</div>
                  <div>...my item here...</div>
             <div>
   </div>

在您的控制器中,您查看了实际ITITEM:

      $scope.oneMessage = false;
      var cpt = 0; // if 1 so you stop to send message
      $scope.$watch('actualItem',function(value){
    if(value.cost == 0 && $scope.oneMessage == false && cpt < 1) 
      // i don't know what is your cost but value is your actual item 
    {
       $scope.oneMessage = true;
       cpt++;
    } 
    else if($scope.oneMessage == true)
      {
         $scope.oneMessage == false;
      }
  });

我不确定这一点,但是您可以尝试。这当然不是最好的方法。

最新更新