【发布时间】:2014-08-08 01:38:50
【问题描述】:
我正在使用 Angular.js 和 CoffeeScript。我在控制器中有一个名为 Current Page 的变量。当前页面绑定到一个文本框。当用户更改当前页面的值时,如何在控制器中触发方法调用。假设当前页面的值为 5。当用户更改它时,我想在 Angular 中调用控制器中的方法.我该怎么做??
提前致谢!!!
【问题讨论】:
标签: javascript angularjs coffeescript
我正在使用 Angular.js 和 CoffeeScript。我在控制器中有一个名为 Current Page 的变量。当前页面绑定到一个文本框。当用户更改当前页面的值时,如何在控制器中触发方法调用。假设当前页面的值为 5。当用户更改它时,我想在 Angular 中调用控制器中的方法.我该怎么做??
提前致谢!!!
【问题讨论】:
标签: javascript angularjs coffeescript
您可以在控制器中使用$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
【讨论】:
我使用了 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)
【讨论】: