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'];
})
})
当我开始在js中使用$ filter选项时,我的订单代码无法正常工作
$scope.order = function(predicate) {
$scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
$scope.predicate = predicate;
};
答案 0 :(得分:0)
您应该使用angular.element
和trigger
方法:
所以:$('.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