Angularjs $scope.$emit在回调中不工作?
我有一个用于身份验证的代码,其中我希望在接收到一些数据后调用事件,但是“$scope.$emit”在“User.me()”的回调中不起作用,我无法理解 有人能解释我吗Angularjs $scope.$emit在回调中不工作?,angularjs,callback,angularjs-scope,emit,Angularjs,Callback,Angularjs Scope,Emit,我有一个用于身份验证的代码,其中我希望在接收到一些数据后调用事件,但是“$scope.$emit”在“User.me()”的回调中不起作用,我无法理解 有人能解释我吗 $scope.login = function () { OAuth.getAccessToken($scope.user).then(function () { $scope.$emit('event:here:work'); User.me({}, {}, function (data
$scope.login = function () {
OAuth.getAccessToken($scope.user).then(function () {
$scope.$emit('event:here:work');
User.me({}, {}, function (data) {
$scope.$emit('event:here:NOT-WORK');
});
$scope.$emit('event:here:work');
}, function (response) {
// error //
});
};
我尝试:
$scope.$emit('event'); // but, not work
$scope.$broadcast('event'); // but, not work
$rootScope.$emit('event'); // but, not work
$rootScope.$broadcast('event'); // work!!!!
现在,“为什么”我不知道?使用
$scope.$broadcast
。实际调用了回调吗?您是否尝试使用成功和错误回调放置日志记录/调试?登录名为success,并且也为“User.me()”。登录回调像预期的那样运行“$scope.$emit”,但是“User.me()”的回调不是分派“$scope.$emit”。这可能是一个愚蠢的问题,但是您在哪里调用回调函数?答案可以从这里找到: