0

I have a code to authentication where I want call an event after receive some data, but "$scope.$emit" not working in the callback of "User.me()" and I not understand.

Anybody can explain me?

$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 //
    });
};

I try:

$scope.$emit('event');          // but, not work
$scope.$broadcast('event');     // but, not work
$rootScope.$emit('event');      // but, not work
$rootScope.$broadcast('event'); // work!!!!

Now, the "why" I not know?

Alex Oliveira
  • 133
  • 1
  • 9
  • Is the callback actually called? Did you try placing logging / debugging using both success and error callback? – CaringDev Mar 22 '16 at 00:22
  • The login is success and "User.me()" too. The login callback run "$scope.$emit" like expected, but the callback of "User.me()" not dispatch "$scope.$emit". – Alex Oliveira Mar 22 '16 at 00:33
  • May be a stupid question but where do you call the callback function? – olysachok Mar 22 '16 at 00:39
  • 1
    The answer can find from here: http://stackoverflow.com/questions/14502006/working-with-scope-emit-and-on – razmik Apr 21 '16 at 09:07

1 Answers1

0

Use $scope.$broadcast instead.

Undo
  • 25,519
  • 37
  • 106
  • 129
olysachok
  • 311
  • 2
  • 8