我有一个控制器(下)。我想从 $animate 的 .then() 方法中更新 newsCtrl 的一部分。但我无法访问它的变量。
从代码中可以看出,我的问题是 currentPage 是“未定义”时
所以我想知道,如何从 .then() 方法中更新 currentPage ?
AngularJS 文档建议我应该使用 $scope.$apply ( https://docs.angularjs.org/error/ $rootScope/inprog?p0=$apply)
但我没有在我的控制器上使用 $scope,我使用的是“this”。
(function(angular, $) {
angular.module('app-news', ['ngAnimate']);
function newsCtrl($animate) {
this.currentPage = 0;
this.cycleNews = function(bool) {
// this.currentPage = 0
$animate.leave(myElem).then(function() {
// this.currentPage = undefined
// $scope.currentPage = undefined
// newsCtrl.currentPage = undefined
$scope.$apply(function(){
// this.currentPage = undefined
// $scope.currentPage = undefined
// newsCtrl.currentPage = undefined
});
});
}
}
angular
.module('app-news')
.controller('newsCtrl', ['$animate', newsCtrl]);
}(window.angular, window.jQuery));