0

what does $scope.$on returns ? I have seen the usage where $scope.$on returns a function and it is again used inside the $on as listener.

 var test = $scope.$on('myEvent', function (evt, data) {
               test();
               $scope.data = true;
            });
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
lavender
  • 67
  • 1
  • 6
  • Possible duplicate of [When to use $scope.$on and $scope.emit on angular?](https://stackoverflow.com/questions/29839917/when-to-use-scope-on-and-scope-emit-on-angular) – Ramesh Rajendran Nov 07 '17 at 08:43
  • Possible duplicate of [Working with $scope.$emit and $scope.$on](https://stackoverflow.com/questions/14502006/working-with-scope-emit-and-scope-on) – Maxim Nov 07 '17 at 10:21

1 Answers1

0

The angular docs are pretty good, so this should be the first place to look at.

Angular docs $scope.$on

Returns a deregistration function for this listener.

So basically with calling the returend function, you destroy your event listener. This would result in a "one-time" listener in your example as your event listener gets deregister after the first call.

Daniel Z.
  • 2,988
  • 2
  • 19
  • 23