Nathan
Nathan

Reputation: 6216

AngularJS perform action after $scope.emit

I need to perform an action after a call to $scope.emit(...) has finished processing (i.e. all the handlers/listeners have completed), how can I do that?

                    $scope.$emit("nsError:setPage", { page: page, ele: ele });// tell page directive to set the current page so that the errored item is visible
                    alert('here');

Currently, the alert happens before the UI is updated to the correct page.

Upvotes: 1

Views: 322

Answers (2)

Alexandre Nucera
Alexandre Nucera

Reputation: 2223

If you emit an event:

 $scope.$emit("nsError:setPage", { page: page, ele: ele })

You could then listen to it :

$scope.$on('nsError:setPage', function(event, mass) { alert('here'); });

Keep in mind that $emit dispatches an event upwards through the scope hierarchy and $broadcast dispatches downwards to your child scopes.

More info in the official doc (scroll to $on)

Upvotes: 0

fikkatra
fikkatra

Reputation: 5792

'$scope.$emit has finished' is not the same thing as 'UI has updated'. For the UI to be updated, the digest cycle must be complete. You can wait for this to happen by using angular's $timeout function (don't forget to inject it in the controller):

$timeout(function(){alert('here');});

Upvotes: 1

Related Questions