I have a $rootScope.$on code on top of a controller. I noticed that everytime I load/call this controller, the $rootScope.$on listener increases meaning it will add and add and add a listener, indefinitely as you visit the controller.
I noticed it when I called it via $rootScope.$emit from another controller, the function inside the $rootScope.$on got executed several times even if it was only a single emit/broadcast.
$rootScope.$on('listen', function() {
$scope.displayString();
});
$scope.displayString = function() {
console.log('test'); // This display 7 times because I visit the controller 7 times
}
Is it possible to prevent it from creating another listener instance so that when there is already a listener, it won't create a new one.
You need to deregister the event listener when you controller's scope is destroyed.
The $on function returns a deregistration function that will remove the listener when the function is invoked.
So you can set it up like this:
var deregister = $rootScope.$on('listen', function() {
$scope.displayString();
});
$scope.$on('$destroy', deregister);
Note: this will only work if the controller's scope is actually destroyed (e.g. in a directive that is removed from the DOM or when you navigate to a different route). If that doesn't happen then you will need to work out a way of only registering the event listener once.
Related
I am using UI router for angular routing. Everytime $state.go() is called a new instance of the controller is being created. I have $rootScope events here. So everytime I want to print any log (using console.log()) or trigger the event from outside the controller it is executing multiple times i.e. the same number of time as the instances of the controller. How can I resolve this issue? I want the execute only one time. Any kind of help is appreciated. Thanks for answer in advance.
code snippet.
$rootScope.$on('connect_device',function () {
connect_device($rootScope.mac_address,$rootScope.device_name);
});
From the different controller I am emitting.
$rootScope.$emit("connect_device")
You are missing some destroyhandling on your controller instance.
The events sunscribed to the $rootScope have to be unsubscribed upon destroy. The controller are indeed created on $state.go, and your eventlistereners are attached each controller creation phase. Not destroying these eventlisteners will cause such behaviour.
Please see following sample code to indicate how you can solve your problem.
angular.module('moduleName')
.controller('controllerName', ['$rootScope', '$scope', function ($rootScope, $scope) {
var cleanUpFunc = $rootScope.$on('eventName', function {
// listener actions
});
$scope.$on('$destroy', function() {
cleanUpFunc();
});
}]);
By calling cleanUpFunc() in $destroy, your event listener for the eventName event will be un-subscribed and you will no longer be leaking memory when your controller gets cleaned up.
When there is no destroyment of events, even when the controller is already destroyed, the eventlisteners will still be active. And as a possible result you will notice that this can invokes multiple functions upon one event.
Off course without any real code examples, the following illustrates how you have to deal with events upon controller destroy.
https://docs.angularjs.org/guide/directive
By listening to this event, you can remove event listeners that might cause memory leaks. Listeners registered to scopes and elements are automatically cleaned up when they are destroyed, but if you registered a listener on a service, or registered a listener on a DOM node that isn't being deleted, you'll have to clean it up yourself or you risk introducing a memory leak.
Best Practice: Directives should clean up after themselves. You can use element.on('$destroy', ...) or scope.$on('$destroy', ...) to run a clean-up function when the directive is removed.
Question:
I have a element.on "click", (event) -> inside my directive:
When the directive is destroyed, are there any memory references to the element.on to keep it from being garbage collected?
Angular documentation states that I should use a handler to remove event listeners on the $destroy emitted event. I was under the impression that destroy() removed event listeners, is this not the case?
Event listeners
First off it's important to understand that there are two kinds of "event listeners":
Scope event listeners registered via $on:
$scope.$on('anEvent', function (event, data) {
...
});
Event handlers attached to elements via for example on or bind:
element.on('click', function (event) {
...
});
$scope.$destroy()
When $scope.$destroy() is executed it will remove all listeners registered via $on on that $scope.
It will not remove DOM elements or any attached event handlers of the second kind.
This means that calling $scope.$destroy() manually from example within a directive's link function will not remove a handler attached via for example element.on, nor the DOM element itself.
element.remove()
Note that remove is a jqLite method (or a jQuery method if jQuery is loaded before AngularjS) and is not available on a standard DOM Element Object.
When element.remove() is executed that element and all of its children will be removed from the DOM together will all event handlers attached via for example element.on.
It will not destroy the $scope associated with the element.
To make it more confusing there is also a jQuery event called $destroy. Sometimes when working with third-party jQuery libraries that remove elements, or if you remove them manually, you might need to perform clean up when that happens:
element.on('$destroy', function () {
scope.$destroy();
});
What to do when a directive is "destroyed"
This depends on how the directive is "destroyed".
A normal case is that a directive is destroyed because ng-view changes the current view. When this happens the ng-view directive will destroy the associated $scope, sever all the references to its parent scope and call remove() on the element.
This means that if that view contains a directive with this in its link function when it's destroyed by ng-view:
scope.$on('anEvent', function () {
...
});
element.on('click', function () {
...
});
Both event listeners will be removed automatically.
However, it's important to note that the code inside these listeners can still cause memory leaks, for example if you have achieved the common JS memory leak pattern circular references.
Even in this normal case of a directive getting destroyed due to a view changing there are things you might need to manually clean up.
For example if you have registered a listener on $rootScope:
var unregisterFn = $rootScope.$on('anEvent', function () {});
scope.$on('$destroy', unregisterFn);
This is needed since $rootScope is never destroyed during the lifetime of the application.
The same goes if you are using another pub/sub implementation that doesn't automatically perform the necessary cleanup when the $scope is destroyed, or if your directive passes callbacks to services.
Another situation would be to cancel $interval/$timeout:
var promise = $interval(function () {}, 1000);
scope.$on('$destroy', function () {
$interval.cancel(promise);
});
If your directive attaches event handlers to elements for example outside the current view, you need to manually clean those up as well:
var windowClick = function () {
...
};
angular.element(window).on('click', windowClick);
scope.$on('$destroy', function () {
angular.element(window).off('click', windowClick);
});
These were some examples of what to do when directives are "destroyed" by Angular, for example by ng-view or ng-if.
If you have custom directives that manage the lifecycle of DOM elements etc. it will of course get more complex.
I have an asynchronous call which is making a change a value on $scope. When it completes, I don't see my view updated, but if I append a $scope.digest() I do see the view update. e.g.
// a doesn't update in view
$rootScope.$on('some_event', function() {
$scope.a = true;
});
// a does update in view
$rootScope.$on('some_event', function() {
$scope.a = true;
$scope.$digest();
});
According to http://www.sitepoint.com/understanding-angulars-apply-digest/ the $digest cycle repeats itself until the $scope has settled (a minimum of two times).
Why wouldn't I see this update?
Thanks!
Dispatching event either by $emit or $broadcast functions call don't start digest cycle itself. Thus, if you have an event dispatched from async code like window.setTimeout or some third-party library, you must use $scope.$apply in event handler or $scope.$digest in code which fire event.
As the title asks. Are they opposite?
Also how can I remove an event from the $rootScope based on its name?
A simple example would be also welcome.
$destroy is both an event that each scope would listen to and also a method on the scope to trigger that event manually.
It is primarily used to do any actions that you want while that scope is getting destroyed. Instances of scope getting destroyed are:
Switching views via ng-view, the previous controller scope gets destroyed.
A directive is instantiated and that element is removed from the DOM, this applies to all directives, within angular framework and custom.
While on the other hand $broadcast is just used to trigger events which are in current level or child level scope.
They are not the opposite.
For the second question on how you can remove the event from $rootScope.
Lets say you have refined an event listener:
$rootScope.$on("myEvent", function () {
//some code
});
Each event listener returns a deregister function. So you can just use something like:
var removeMyEvent = $rootScope.$on("myEvent", function () {
//some code
});
Whenever you want to remove the event listener you just call the deregister function.
removeMyEvent();
That should do the trick!
Is it recommended to always hang on to the $on method, as in
var dereg = $scope.$on(...)
And later
$scope.$on("$destroy", function() { dereg(); });
Or is this only needed for certain situations? Same question for $watch
AFAIK, you should only use $destroy event when your app may have memory leak issues or zombie events.
$destroy is useful when you pass some of the directive's variables to another directive or controller, and after the element gets removed, yo want to remove it from the reference.
For instance, you may want to use $destory when your directive creates event handlers on global DOM elements and the element with the directive gets removed.
let's say this is a linking function inside a directive:
function myEventListener(){console.log('scroll!');}
$window.bind('mousewheel',myEventListener);
scope.$on('$destroy',function(){$window.unbind(myEventListener)});
If you don't unbind your event listener, then after the element with the directive gets removed, you will still have scroll! messages when you scroll.