Angularjs订单不正常

时间:2017-03-29 02:36:38

标签: javascript angularjs ionic-framework

我面临着一个小问题,因为我无法理解 当我重新加载数组时,我可以看到每件事情都完美无缺,但问题在于列表的ng-click功能。
例如,如果我有这个数组:

$scope.ticketsEncour = [
  {_id: 1, vendeur: 'John Doe', date_ticket: '18:52', total: 50},
  {_id: 2, vendeur: 'Foo Bar', date_ticket: '12:12', total: 20},
  {_id: 3, vendeur: 'John Smith', date_ticket: '11:02', total: 10},
  {_id: 4, vendeur: 'Test Test', date_ticket: '05:10', total: 6000}
]

这是重新登记和点击功能:

$scope.reverse = false;
$scope.orderItems = function (item) {
$scope.reverse = !$scope.reverse;
  if (item != undefined) {
    if (item == $scope.orderItem) {
      $scope.orderType = $scope.orderType == '+' ? '-' : '+';
    }
    else {
      $scope.orderItem = item;
      $scope.orderType = '+';
    }
  }

  $('.iconOrder').removeClass('up').removeClass('down');
  if ($scope.orderType == '+') {
    $('.iconOrder[name="' + $scope.orderItem + '"]').addClass('down')
  }
  else {
    $('.iconOrder[name="' + $scope.orderItem + '"]').addClass('up')
  }

  return $scope.orderType + $scope.orderItem;
}
$scope.goToCaissePage = function (id) {
  id = id.replace("app/caisse/", "");
  console.log(id);
};

我的Html代码

<ion-list class="list articlesList swipedList" show-delete="showDelete">
      <ion-item class="item item-button-right stable-bg">
        <span class="row">
          <span class="col col-20" ng-click="orderItems('vendeur')">
            <strong>Vendeur</strong>
            <div class="iconOrder" name="vendeur">
              <i class="icon ion-android-arrow-dropup"></i>
              <i class="icon ion-android-arrow-dropdown"></i>
            </div>
          </span>
          <span class="col col-20" ng-click="orderItems('date_ticket')">
            <strong>Heure</strong>
            <div class="iconOrder" name="date_ticket">
              <i class="icon ion-android-arrow-dropup"></i>
              <i class="icon ion-android-arrow-dropdown"></i>
            </div>
          </span>
          <span class="col col-10" ng-click="orderItems('total')">
            <strong>Total</strong>
            <div class="iconOrder" name="total">
              <i class="icon ion-android-arrow-dropup"></i>
              <i class="icon ion-android-arrow-dropdown"></i>
            </div>
          </span>
        </span>
      </ion-item>


      <ion-item class="item item-button-right pad-item" ng-repeat="ticket in ticketsEncour | orderBy : orderBy : orderItem:reverse  track by $index">
        <span class="row">
          <span class="col col-20 col-pad" ng-click="goToCaissePage('app/caisse/{{ticket._id}}')">
            <div>
              <a href="" style="color: #333; text-decoration: none">{{ ticket.vendeur }}</a>
            </div>
          </span>
          <span class="col col-20 col-pad" ng-click="goToCaissePage('app/caisse/{{ticket._id}}')">
            <div>
              <a href="" style="color: #333; text-decoration: none">{{ ticket.date_ticket | date:'HH:mm' }}</a>
            </div>
          </span>
          <span class="col col-10 col-pad" ng-click="goToCaissePage('app/caisse/{{ticket._id}}')">
            <div>
              <a href="" style="color: #333; text-decoration: none">{{ ticket.total.toFixed(2) }}€</a>
            </div>
          </span>
        </span>
      </ion-item>
    </ion-list>

当我点击ng-repeat的任何项目时,它会在控制台中显示正确的ID。但是,当我使用重新排序时,我可以改变HTML但是ng-click功能总是呈现旧的:

点击索引0 =&gt;控制台1(完美)
以任何类型重新登记后:
点击索引0 =&gt;控制台1(例如预期3)

由于

更新:更改了阵列名称(复制剪切错误)
更新了@Weijian建议

2 个答案:

答案 0 :(得分:0)

我建议使用直接使用范围变量的orderBy。这也是角度文档建议的方法。

JS

$scope.reverse = false;
$scope.orderItems = function (item) {
    if( item == $scope.orderItem ){
        $scope.reverse = !$scope.reverse;
    }
    $scope.orderItem = item;
    $('.iconOrder').removeClass('up').removeClass('down');        
    if( $scope.reverse ){        
        $('.iconOrder[name="' + $scope.orderItem + '"]').addClass('down');
    }
    else {
        $('.iconOrder[name="' + $scope.orderItem + '"]').addClass('up');
    }
}

HTML

orderBy:orderItem:reverse

https://docs.angularjs.org/api/ng/filter/orderBy

这种方式更清晰,因为你避免混合使用orderItems函数。

答案 1 :(得分:0)

我修复了问题,我来自track by $index循环中的ng-repeat