order by and click在ng repeat angular js中不起作用

order by and click is not working in ng-repeat angular js

本文关键字:angular js 不起作用 repeat ng by and click order      更新时间:2023-09-26

ng重复显示数据库中的数据,但我的谓词顺序、反向功能和点击不起作用

<div ng-repeat="shipment in sortedShipments">
  <div ng-click="show_shipment($index,shipment.shipment_id)" ng-class="{selected_trip: $index === currentShipment}">
     <div>{{shipment.from_location}</div>
  </div>
</div>

这是从数据库HTTP请求和排序代码中获取记录

$http.get(url+'/get-shipments').success(function(data){
  $scope.shipments = data;
  $scope.sortedShipments = $filter('orderBy')($scope.shipments, 'predicate', true);
  $scope.currentShipment = 0;
  $scope.sortType  = 'shipment_id';
  $scope.predicate = 'created_at';
  $scope.reverse = false;
});

对于点击功能,我有两个按钮可以使用选定的索引和上一个按钮移动到下一个发货div

<a class="" ng-click="next($event)"  href="#">next</a>
<a class="" ng-click="previous($event)"  href="#">previous</a>

角js码是

$scope.next = function($event) {
  if ($scope.currentShipment < $scope.sortedShipments.length - 1) {
       $scope.currentShipment++;
       $('.selected_trip').click();
  }
};
$scope.previous = function($event) {
  if ($scope.currentShipment > 0) {
      $scope.currentShipment--;
      $('.selected_trip').click();
  }
};

问题是它进入下一个项目,在上面添加selected_trip类,但我也想触发对所选类的点击,这样它就可以在所选行ng-click="show_shipment($index,shipment.shipment_id)"上调用这个函数但目前$('.selected_trip').click();这给了我错误我也在上试过这个

document.querySelector('.selected_trip').click();

这是展示发货方式

 $scope.show_shipment = function(index,id) {
        $scope.setSelected(index); 
        $http.get(url+'/get-shipments/'+id).success(function(data){
           $scope.to_location = data[0]['to_location'];
        })
})

这是我的订单代码,当我开始在js 中使用$filter选项时,它不起作用

 $scope.order = function(predicate) {
      $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
      $scope.predicate = predicate;
    };

您应该使用angular.elementtrigger方法:

所以这个:$('.selected_trip').click();

成为:angular.element('.selected_trip').trigger('click');

它应该起作用。

编辑

您还应该使用$timeout作为解决方法,因为您还处于$digest中,并且不能再次调用$apply,这就是您出现这些错误的原因。

我制作了一个JSFiddle来向您解释$timeout:的使用

app.controller('dummy', function ($scope, $timeout) {
    $scope.test = function () {
        $timeout(function() {
            angular.element('.selected_trip').trigger('click');
        }, 0, false);
    }
    $scope.show_shipment = function () {
        $scope.clicked = "hello world";
    };
});

更多信息请点击此处:https://docs.angularjs.org/error/$rootScope/inprog?p0=$应用