And*_*sta 1 angularjs angularjs-directive
我有一个指令可以为我生成一个列表。该指令具有分页方法。我想使用键盘控制此列表,以便当我向左或向右按列表页面的下一页或上一页时。
反正我能做到吗?
下面是我的指令代码:
app.directive("gridview", function(){
return {
restrict:'E',
transclude: true,
template:'<div><button ng-disabled="!hasPrevious()" ng-click="onPrev()"> Previous </button><button ng-disabled="!hasNext()" ng-click="onNext()"> Next </button></div><div ng-transclude></div>',
scope:{
'currentItem':'=',
'currentPage':'=',
'pageLimit':'=',
'data':'=',
'totalPages' :'&'
},
link:function($scope, element, attrs){
$scope.size = function(){
return angular.isDefined($scope.data) ? $scope.data.length : 0;
};
$scope.end = function(){
return $scope.start() + $scope.pageLimit;
};
$scope.start = function(){
return $scope.currentPage * $scope.pageLimit;
};
$scope.totalPages = function(){
$scope.totalPages({theTotal : $scope.size});
};
$scope.page = function(){
return !!$scope.size() ? ( $scope.currentPage + 1 ) : 0;
};
$scope.hasNext = function(){
return $scope.page() < ( $scope.size() / $scope.pageLimit ) ;
};
$scope.onNext = function(){
$scope.currentPage = parseInt($scope.currentPage) + 1;
$scope.currentItem = parseInt($scope.currentPage) + 1;
};
$scope.hasPrevious = function(){
return !!$scope.currentPage;
} ;
$scope.onPrev = function(){
$scope.currentPage=$scope.currentPage-1;
};
}
}
});
Run Code Online (Sandbox Code Playgroud)
您始终可以使用 Angular 事件系统,它允许您在控制器和指令之间进行交互。
只需在您的控制器中广播一个事件并在您的指令中收听它以调用您想要的函数。
控制器
$scope.$broadcast('onPrev')
Run Code Online (Sandbox Code Playgroud)
指示
$scope.$on('onPrev', function () {
$scope.onPrev()
})
Run Code Online (Sandbox Code Playgroud)
这是主要问题,但你更具体的问题,你应该看看ngKeypress
| 归档时间: |
|
| 查看次数: |
4076 次 |
| 最近记录: |