Can someone help me understand the way when we should use $rootScope.$on
and $scope.$on
.
I know that its mostly for hearing different scope ($rootScope and $scope).
My query is for below Scenario:
Shall I use : $rootScope.$emit with $rootScope.$on
OR
Shall I prefer: $rootScope.$broadcast with $scope.$on I know this will be not a good option as it'll broadcast to all
$scope
obj.
OR
Shall I go for: $rootScope.$broadcast with $rootScope.$on
As you can see, I need to handle event on $rootScope level.
What is the difference in above 3 implementations ?
Root ScopeAll applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.
$rootScope exists, but it can be used for evilScopes in Angular form a hierarchy, prototypally inheriting from a root scope at the top of the tree. Usually this can be ignored, since most views have a controller, and therefore a scope, of their own.
In short, in case of dependency injection the scope object is received as $scope while in case of non-dependency injection scope object is received as scope or with any name. Save this answer.
This function is used to execute an expression in Agular. The function expression is optional and you can directly use $apply(). This is used to run watcher for the entire scope. $rootScope.$digest() This is used to call watcher for the entire scope.
This is a good questions and there is an explanation for you.
$scope.on('event');
will listen to $scope.$broadcast('event')
& $rootScope.$broadcast('event')
$rootScope.on('event');
will listen to $rootScope.$broadcast('event')
& $rootScope.$emit('event')
$scope.on();
will be destroyed automatically when the controller loses it representation in view or component (getting destroyed). $rootScope.$on()
manually.$rootScope.on()
://bind event
var registerScope = $rootScope.$on('someEvent', function(event) {
console.log("fired");
});
// auto clean up `$rootScope` listener when controller getting destroy
// listeners will be destroyed by calling the returned function like registerScope();
$scope.$on('$destroy', registerScope);
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', function ($scope, $rootScope) {
var registerScope = null;
this.$onInit = function () {
//register rootScope event
registerScope = $rootScope.$on('someEvent', function(event) {
console.log("fired");
});
}
this.$onDestroy = function () {
//unregister rootScope event by calling the return function
registerScope();
}
});
$scope.on()
and $rootScope.on()
.By switching the view in this plunkr the controller will be rebinded to your view. The $rootScope.on();
event is binded every time you switch a view without destroying the event bindings of the view before. In that way the $rootScope.on()
listeners will be stacked/multiplied. This will not happen to the $scope.on()
bindings because it will be destroyed by switching the view (losing the E2E binding representation in DOM -> controller is destroyed).
$emit
& $broadcast
is:$rootScope.$emit()
events only triggers $rootScope.$on()
events. $rootScope.$broadcast()
will trigger $rootScope.$on()
& $scope.on()
events (pretty much everthing hear this event).$scope.$emit()
will trigger all $scope.$on
, all its parents (scopes in parent controllers) and $rootScope.$on()
.$scope.$broadcast
will only trigger $scope
and its children (scopes in child controllers). If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With