1

I am using Angular.js and CoffeeScript. I have a variable in the controller called Current Page. The Current Page is bound to a text box. How do I trigger a method call in the controller when a user changes the value of Current Page.i.e Let's say the value of the current page is 5. When the user changes it, I want to a call a method in the controller in Angular. How do I do this ??

Thanks in advance !!!

SaiBand
  • 5,025
  • 15
  • 57
  • 76

2 Answers2

2

you can use $scope.$watch. in your controller.

  $scope.currentPage = 1;

   $scope.$watch('currentPage', function() {
       alert('hey, currentPage has changed!');
   }, true);

Check out Using scope.$watch and scope.$apply and $watch an object in angular

Community
  • 1
  • 1
Christopher Marshall
  • 10,678
  • 10
  • 55
  • 94
0

I used ng-blur and called a method in my controller. using $scope.$watch it fires even as the user is entering. Hence used ng-blur.

<input type="text" ng-model="controller.currentPage" ng-blur="controller.handlePageChange()" class="form-  control"/><span>Of</span>

In the controller I have this:

  handlePageChange:=>
    if (this.currentPage != this.data.currentPage) && (this.currentPage >= 1 && this.currentPage <= this.data.maxPages())
      console.log 'The page has indeed changed'
      this.data.paginate(this.currentPage)
SaiBand
  • 5,025
  • 15
  • 57
  • 76