Skip to main content
Ben Nadel at Scotch On The Rocks (SOTR) 2011 (Edinburgh) with: James Allen
Ben Nadel at Scotch On The Rocks (SOTR) 2011 (Edinburgh) with: James Allen ( @CFJamesAllen )

Unbinding Scope.$on() Event Handlers In AngularJS

Published in Comments (1)

Most of the time, we don't have to think about unbinding Scope.$on() event handlers in AngularJS because they are implicitly unbound when the current scope is destroyed. But, if you think about the scope tree as a built-in pub/sub (Publish and Subscribe) mechanism, you can imagine scenarios in which it would be useful to explicitly unbind an event handler. To do so, we use the same approach that we needed in order to unbind Scope.$watch() event handlers - we store a reference to the deregistration function.

Run this demo in my JavaScript Demos project on GitHub.

When you call the Scope.$on() method, in AngularJS, the return value is a deregistration function for the bound event handler. When you invoke that deregistration function, it's basically like calling .off() on that event handler - it unbinds it (even though there is no .off() method).

To see this in action, I have two instances of a Controller that are listening for a "ping" event that is being continuously $broadcast() from the parent $scope. When you click on either of the relevant Views, the Controller will toggle the event binding and stop updating the view-model:

<!doctype html>
<html ng-app="Demo">
	<meta charset="utf-8" />

		Unbinding Scope.$on() Event Handlers In AngularJS

	<link rel="stylesheet" type="text/css" href="./demo.css"></link>
<body ng-controller="AppController">

		Unbinding Scope.$on() Event Handlers In AngularJS

		class="event-target left"
		ng-class="{ active: isWatchingEvent }">

		{{ eventCount }}


		class="event-target right"
		ng-class="{ active: isWatchingEvent }">

		{{ eventCount }}


	<!-- Load scripts. -->
	<script type="text/javascript" src="../../vendor/jquery/jquery-2.1.0.min.js"></script>
	<script type="text/javascript" src="../../vendor/angularjs/angular-1.2.26.min.js"></script>
	<script type="text/javascript">

		// Create an application module for our demo.
		var app = angular.module( "Demo", [] );

		// -------------------------------------------------- //
		// -------------------------------------------------- //

		// I control the root of the application.
			function( $scope, $interval ) {

				// Continuously broadcast an event down the scope tree.
					function handleInterval() {

						$scope.$broadcast( "ping" );



		// -------------------------------------------------- //
		// -------------------------------------------------- //

		// I control the event targets.
			function( $scope ) {

				// I keep track of the number of times we've handled a given event.
				$scope.eventCount = 0;

				// I keep track of whether or not this controller is currently listening
				// for the broadcast event.
				$scope.isWatchingEvent = false;

				// I am the deregistration method for the event handler. This is the
				// closest thing we have to a scope.$off() method.
				var unbindHandler = null;

				// When the controller loads, start listening for broadcast events.

				// ---
				// ---

				// I turn on / off event listening depending on the current state.
				$scope.toggleListener = function() {

						? stopWatchingEvent()
						: startWatchingEvent()


				// ---
				// ---

				// I respond to the "ping" event on the scope.
				function handlePingEvent( event ) {



				// I start watching for the "ping" event on the scope.
				function startWatchingEvent() {

					// When we bind the $on() event, the return value is the
					// deregistration method for the event handler. This is the way we
					// can handle unbind event handlers without destroying the scope.
					// --
					// NOTE: When a scope is $destroy()'d, it will automatically unbind
					// all of your event handlers.
					unbindHandler = $scope.$on( "ping", handlePingEvent );

					$scope.isWatchingEvent = true;


				// I stop watching for the "ping" event on the scope.
				function stopWatchingEvent() {

					// Invoke the deregistration method in order to unbind the event
					// handler. Set to null so we know how to handle the "toggle" method.
					unbindHandler = null;

					$scope.isWatchingEvent = false;





As you can see, when the event is bound, the return value - the deregistration function - is stored. And, when we need to unbind the event, all we do is invoke the deregistration function.

Want to use code from this post? Check out the license.

Reader Comments



I've taken this a bit further to create a "run once" event handler:

var deregisterRunOnceHandler = $scope.$on('eventName', function() {
deregisterRunOnceHandler = null;

Can be useful for others as well.



I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel