Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  14] [ 2]  / answers: 1 / hits: 12176  / 11 Years ago, wed, december 18, 2013, 12:00:00

I am trying to create a simple pagination directive with an isolated scope. For some reason when I manually change the value it gets a bit finnicky. Here is my problem:



When I page forward and backward, it works great. Awesome



When I enter a page into the field it works. Great



However, if I enter a page into the field and then try to go forward and backward, the ng-model seems to break after I enter a page into the field. I had it working when I did not isolate my scope but I am confused as to why it would break it. Here is my code:



HTML:



<paginate go-to-page=goToPage(page) total-pages=results.hits.pages total-hits=results.hits.total></paginate>


Directive:



'use strict';

angular.module('facet.directives')
.directive('paginate', function(){
return {
restrict: 'E',
template: '<div class=pull-right ng-if=(totalPages !== undefined) && (totalPages > 0)>'+
'<span class=left-caret hoverable ng-click=changePage(current-1) ng-show=current > 1></span>&nbsp;&nbsp;Page'+
'&nbsp;&nbsp;&nbsp;<input type=number ng-model=current class=pagination-input ng-keypress=enterPage($event)/> of'+
'&nbsp;&nbsp;&nbsp;{{totalPages}}&nbsp;&nbsp;'+
'<span class=right-caret hoverable ng-click=changePage(current+1) ng-show=current < totalPages></span>'+
'</div>',
scope: {
goToPage: '&',
totalPages: '=',
totalHits: '='
},
link: function(scope) {
scope.current = 1;
scope.changePage = function(page) {
scope.current = page;
window.scrollTo(0,0);
scope.goToPage({page:page});
};
scope.enterPage = function(event) {
if(event.keyCode == 13) {
scope.changePage(scope.current);
}
}
}
}
});


What am I doing wrong?


More From » angularjs

 Answers
1

Please always try to use model rather than using primitive types while using the ng-model because of the javascript's prototypical hierarchies.



angular.module('facet.directives').directive('paginate', function () {
return {
restrict: 'E',
replace: true,
template: '<div class=pull-right discovery-pagination ng-if=(totalPages !== undefined) && (totalPages > 0)>' +
'<span class=left-caret hoverable ng-click=changePage(current-1) ng-show=current > 1></span>&nbsp;&nbsp;Page' +
'&nbsp;&nbsp;&nbsp;<input type=number ng-model=current.paging class=pagination-input ng-keypress=enterPage($event)/> of' +
'&nbsp;&nbsp;&nbsp;{{totalPages}}&nbsp;&nbsp;' +
'<span class=right-caret hoverable ng-click=changePage(current+1) ng-show=current < totalPages></span>' +
'</div>',
scope: {
goToPage: '&',
totalPages: '=',
totalHits: '='
},
link: function(scope) {
scope.current = {paging:1};
scope.changePage = function(page) {
scope.current.paging = page;
window.scrollTo(0, 0);
scope.goToPage({ page: page });
};
scope.enterPage = function(event) {
if (event.keyCode == 13) {
scope.changePage(scope.current.paging);
}
};
}
};
});


Hope this will solve your problem :)



For detail about this, please go through Understanding-Scopes


[#49432] Tuesday, December 17, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samir

Total Points: 145
Total Questions: 90
Total Answers: 89

Location: Tokelau
Member since Sun, May 7, 2023
1 Year ago
;