【问题标题】:How to have an Observable in Coffee Script and Angular如何在 Coffee Script 和 Angular 中有一个 Observable
【发布时间】:2014-08-08 01:38:50
【问题描述】:

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

提前致谢!!!

【问题讨论】:

    标签: javascript angularjs coffeescript


    【解决方案1】:

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

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

    查看Using scope.$watch and scope.$apply$watch an object in angular

    【讨论】:

    • 感谢您的回复。这给了我正确的工作方向。
    【解决方案2】:

    我使用了 ng-blur 并在我的控制器中调用了一个方法。使用 $scope.$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)
    

    【讨论】:

      猜你喜欢
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      • 2015-01-08
      • 2015-03-13
      • 2014-03-30
      • 2018-08-04
      • 2016-11-16
      相关资源
      最近更新 更多