I'm trying to implement a simple <pagination> directive in Angular JS, and having trouble getting it to communicate with another controller to get it to react on changes to the current page.
Here's what my code looks like:
paginationDirective.js
app.controller('paginationCtrl', function ($scope) {
this.init = function (attrs) {
$scope.items = [
{'page': '1', class: 'current'},
{'page': '2', class: ''},
{'page': '3', class: ''},
{'page': '4', class: ''},
{'page': '5', class: ''}
];
};
});
app.directive('pagination', function () {
return {
controller: 'paginationCtrl',
restrict: 'E',
scope: {
count: '@',
page: '@'
},
templateUrl: 'views/partials/pagination.html',
link: function (scope, element, attrs, paginationCtrl) {
paginationCtrl.init(attrs);
}
}
});
pagination.html
<div class="pagination-centered">
<ul class="pagination">
<li class="arrow unavailable"><a href="">«</a></li>
<li ng-repeat="item in items" class="{{ item.class }}">
<a href="" ng-click="<!-- somepageCtrl.navigate(item.page) -->">{{ item.page }}</a>
</li>
<li class="arrow"><a href="">»</a></li>
</ul>
</div>
somepage.html
<div class="content">
<table>...data for page x...</table>
<pagination onpage=" <!-- Run somepageCtrl.page(x) --> "></pagination>
</div>
somepageCtrl.js
app.controller('somepageCtrl', function($scope, myService){
$scope.currentPage = 1;
$scope.init = function () {
myService.loadData($scope);
};
$scope.navigate = function(page) {
console.log('Navigating to page ' + page);
}
$scope.init();
});
I'm still new to AngularJS, so I'm not entirely sure what I should do to get the pagination directive to tell somepageCtrl to navigate to the next page.
I would like to be able to pass a function that needs to be executed when the user clicks an item in the pagination directive as an attribute on the directive.
Can anyone help and nudge me in the right direction?
somepageCtrlset?<pagination onpage=" <!-- Run somepageCtrl.page(x) --> "></pagination>did you meansomepageCtrl.navigate(x)?