如何在Coffee Script和Angular中拥有一个可观察对象

How to have an Observable in Coffee Script and Angular

本文关键字:有一个 拥有 观察 对象 Angular Coffee Script      更新时间:2023-09-26

我使用的是Angular.js和CoffeeScript。我在控制器中有一个变量叫Current Page。"当前页"绑定到一个文本框。当用户改变当前页面的值时,我如何在控制器中触发方法调用?例如,假设当前页面的值为5。当用户改变它时,我想在Angular的控制器中调用一个方法。我该怎么做??

提前感谢!!

您可以在控制器中使用$scope.$watch.

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

检查Using作用域。$手表和范围。

我使用ng-blur并在我的控制器中调用一个方法。使用美元的范围。$watch在用户输入时触发。因此使用了ng-blur。

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

在控制器中,我有这个:

  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)
相关文章: