gpt4 book ai didi

javascript - order by 和 click 在 ng-repeat Angular JS 中不起作用

转载 作者:行者123 更新时间:2023-11-28 06:39:41 25 4
gpt4 key购买 nike

ng-repeat 显示数据库中的数据,但我按谓词排序、反向功能和单击不起作用

<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>

而 Angular 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();

这里是show_shipment方法

 $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'];
})
})

这是我的 order by 代码,当我开始在 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=$apply

关于javascript - order by 和 click 在 ng-repeat Angular JS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com