Angular.js: Is it possible to capture and console log all broadcasts?

Created on 29 Jan 2014  ·  3Comments  ·  Source: angular/angular.js

This is what I have tried:

$rootScope.$on("$broadcast", function ($event) {
    console.log("BROADCAST: " + $event.name);
});

Is this something that is (couldn't find any docs for it) or will ever be implemented or is the overhead too great?

My thinking originated from the $route event docs, e.g. $routeChangeSuccess.

Most helpful comment

This is not something which is likely to ever be implemented, as you say, due to the overhead.

However, it is possible to implement a notifier in your own app, so that you can create a way to notify your own app using decorators.

app.config(function($provide) {
  $provide.decorator("$rootScope", function($delegate) {
    var Scope = $delegate.constructor;
    var origBroadcast = Scope.prototype.$broadcast;
    var origEmit = Scope.prototype.$emit;

    Scope.prototype.$broadcast = function() {
      console.log("$broadcast was called on $scope " + $scope.$id + " with arguments:",
                         arguments);
      return origBroadcast.apply(this, arguments);
    };
    Scope.prototype.$emit = function() {
      console.log("$emit was called on $scope " + $scope.$id + " with arguments:",
                         arguments);
      return origEmit.apply(this, arguments);
    };
    return $delegate;
  });
});

Here's a quick example: http://plnkr.co/edit/cn3MZynbpTYIcKUWmsBi?p=preview

All 3 comments

This is not something which is likely to ever be implemented, as you say, due to the overhead.

However, it is possible to implement a notifier in your own app, so that you can create a way to notify your own app using decorators.

app.config(function($provide) {
  $provide.decorator("$rootScope", function($delegate) {
    var Scope = $delegate.constructor;
    var origBroadcast = Scope.prototype.$broadcast;
    var origEmit = Scope.prototype.$emit;

    Scope.prototype.$broadcast = function() {
      console.log("$broadcast was called on $scope " + $scope.$id + " with arguments:",
                         arguments);
      return origBroadcast.apply(this, arguments);
    };
    Scope.prototype.$emit = function() {
      console.log("$emit was called on $scope " + $scope.$id + " with arguments:",
                         arguments);
      return origEmit.apply(this, arguments);
    };
    return $delegate;
  });
});

Here's a quick example: http://plnkr.co/edit/cn3MZynbpTYIcKUWmsBi?p=preview

In the future, please consider asking the mailing list, IRC channel or stackoverflow before posting an issue, however --- this is primarily meant for bug tracking.

Wow, nice. Thanks Caitlin.

Also, noted; my mistake

On Wednesday, 29 January 2014, Caitlin Potter [email protected]
wrote:

In the future, please consider asking the mailing listhttps://groups.google.com/forum/#!forum/angular,
IRC channel http://webchat.freenode.net/?channels=angularjs or
stackoverflow http://stackoverflow.com/questions/tagged/angularjsbefore posting an issue, however --- this is primarily meant for bug
tracking.


Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/6043#issuecomment-33610626
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

visnup picture visnup  ·  3Comments

WesleyKapow picture WesleyKapow  ·  3Comments

kishanmundha picture kishanmundha  ·  3Comments

nosideeffects picture nosideeffects  ·  3Comments

michael-letcher picture michael-letcher  ·  3Comments