$ scope。$ emit和$ rootScope有什么区别。$ emit?

时间:2022-12-15 19:30:24

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!

我正在使用它从指令发出控制器,它可以两种方式工作!

3 个解决方案

#1


1  

$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监听特定事件。

#2


0  

$scope.$emit is useful when you want that $scope and all its parents and $rootScope able to hear the event. $scope.$emit is a child whining to their parents $scope.

当你希望$ scope及其所有父项和$ rootScope能够听到这个事件时,$ scope。$ emit非常有用。 $ scope。$ emit是一个抱怨父母$ scope的孩子。

And

$rootScope.$emit only lets other $rootScope listeners for catching it. This is useful when you don't want to notify every $scope.

$ rootScope。$ emit只允许其他$ rootScope监听器捕获它。当您不想通知每个$ scope时,这很有用。

#3


-1  

$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.
It basically works like childScope > parentScope(s).

因此,当您执行$ scope。$ emit时,您将向父作用域发送通知,该作用域可能是另一个$ scope或者可能是$ rootScope。它基本上像childScope> parentScope一样工作。

Its opposite is .broadcast().

它的对面是.broadcast()。

.broadcast() broadcasts changes to child scopes. So, if you broadcast from $rootScope then all the child $scopes listen.

.broadcast()广播对子范围的更改。因此,如果您从$ rootScope广播,那么所有子$ scopes都会监听。


From the docs:

$emit(name, args);

Dispatches an event name upwards through the scope hierarchy notifying the registered $rootScope.Scope listeners.

通过范围层次结构向上调度事件名称,通知已注册的$ rootScope.Scope侦听器。

The event life cycle starts at the scope on which $emit was called. 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.

事件生命周期从调用$ emit的范围开始。监听此范围内的名称事件的所有侦听器都会收到通知。之后,事件向上遍历根范围并沿途调用所有已注册的侦听器。如果其中一个侦听器取消它,该事件将停止传播。

Any exception emitted from the listeners will be passed onto the $exceptionHandler service.

从侦听器发出的任何异常都将传递给$ exceptionHandler服务。

#1


1  

$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监听特定事件。

#2


0  

$scope.$emit is useful when you want that $scope and all its parents and $rootScope able to hear the event. $scope.$emit is a child whining to their parents $scope.

当你希望$ scope及其所有父项和$ rootScope能够听到这个事件时,$ scope。$ emit非常有用。 $ scope。$ emit是一个抱怨父母$ scope的孩子。

And

$rootScope.$emit only lets other $rootScope listeners for catching it. This is useful when you don't want to notify every $scope.

$ rootScope。$ emit只允许其他$ rootScope监听器捕获它。当您不想通知每个$ scope时,这很有用。

#3


-1  

$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.
It basically works like childScope > parentScope(s).

因此,当您执行$ scope。$ emit时,您将向父作用域发送通知,该作用域可能是另一个$ scope或者可能是$ rootScope。它基本上像childScope> parentScope一样工作。

Its opposite is .broadcast().

它的对面是.broadcast()。

.broadcast() broadcasts changes to child scopes. So, if you broadcast from $rootScope then all the child $scopes listen.

.broadcast()广播对子范围的更改。因此,如果您从$ rootScope广播,那么所有子$ scopes都会监听。


From the docs:

$emit(name, args);

Dispatches an event name upwards through the scope hierarchy notifying the registered $rootScope.Scope listeners.

通过范围层次结构向上调度事件名称,通知已注册的$ rootScope.Scope侦听器。

The event life cycle starts at the scope on which $emit was called. 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.

事件生命周期从调用$ emit的范围开始。监听此范围内的名称事件的所有侦听器都会收到通知。之后,事件向上遍历根范围并沿途调用所有已注册的侦听器。如果其中一个侦听器取消它,该事件将停止传播。

Any exception emitted from the listeners will be passed onto the $exceptionHandler service.

从侦听器发出的任何异常都将传递给$ exceptionHandler服务。