[英]What is difference between $scope.$emit and $rootScope.$emit?
What is difference between $scope.$emit and $rootScope.$emit ? $ scope。$ emit和$ rootScope。$ emit有什么区别?
I'm using it to emit from a directive to controller and it works un both ways! 我正在使用它从指令发出到控制器,并且它在两种情况下都起作用!
$scope.$emit
allows the current scope and parents (including the rootScope) to listen for an event. $scope.$emit
允许当前作用域和父作用域(包括rootScope)侦听事件。
$rootScope.$emit
allows only the rootScope to listen for a specific event. $rootScope.$emit
仅允许rootScope侦听特定事件。
$scope.$emit
is useful when you want that $scope
and all its parents and $rootScope
able to hear the event. 当您希望该$scope
及其所有父级和$rootScope
能够听到该事件时, $scope.$emit
很有用。 $scope.$emit
is a child whining to their parents $scope
. $scope.$emit
是一个对父母$scope
抱怨的孩子。
And 和
$rootScope.$emit
only lets other $rootScope
listeners for catching it. $rootScope.$emit
仅允许其他$rootScope
侦听器捕获它。 This is useful when you don't want to notify every $scope
. 当您不想通知每个$scope
时,这很有用。
$emit()
sends changes to its parent scopes upward in the chain. $emit()
将更改向上发送到其父作用域。
So, when you do $scope.$emit
, you are sending a notification to the parent scope, which could be another $scope
or might be $rootScope
. 因此,当您执行$scope.$emit
,您正在向父范围发送通知,该通知可能是另一个$scope
或可能是$rootScope
。
It basically works like childScope > parentScope(s)
. 它基本上像childScope > parentScope(s)
。
Its opposite is .broadcast()
. 相反的是.broadcast()
。
.broadcast()
broadcasts changes to child scopes. .broadcast()
广播对子作用域的更改。 So, if you broadcast from $rootScope
then all the child $scope
s listen. 因此,如果您从$rootScope
广播,则所有子级$scope
都将监听。
$emit(name, args); $ emit(name,args);
Dispatches an event
name
upwards through the scope hierarchy notifying the registered $rootScope.Scope listeners. 通过作用域层次结构向上调度事件name
通知已注册的$ rootScope.Scope侦听器。The event life cycle starts at the scope on which $emit was called. 事件生命周期始于调用$ emit的作用域。 All listeners listening for name event on this scope get notified. 在此作用域上侦听名称事件的所有侦听器都会收到通知。 Afterwards, the event traverses upwards toward the root scope and calls all registered listeners along the way. 然后,事件向上遍历到根作用域,并在此过程中调用所有已注册的侦听器。 The event will stop propagating if one of the listeners cancels it. 如果其中一个侦听器取消了该事件,则该事件将停止传播。
Any exception emitted from the listeners will be passed onto the $exceptionHandler service. 侦听器发出的任何异常都将传递到$ exceptionHandler服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.