本文翻译自:Working with $scope.$emit and $scope.$on
How can I send my $scope
object from one controller to another using .$emit
and .$on
methods? 如何使用.$emit
和.$on
方法将$scope
对象从一个控制器发送到另一个控制器?
function firstCtrl($scope) {
$scope.$emit('someEvent', [1,2,3]);
}
function secondCtrl($scope) {
$scope.$on('someEvent', function(mass) { console.log(mass); });
}
It doesn't work the way I think it should. 它不像我认为的那样工作。 How do $emit
and $on
work? $emit
和$on
work如何运作?
#1楼
参考:https://stackoom.com/question/yqdK/使用-scope-emit和-scope-on
#2楼
First of all, parent-child scope relation does matter. 首先,父子范围关系确实很重要。 You have two possibilities to emit some event: 你有两种可能发出一些事件:
-
$broadcast
-- dispatches the event downwards to all child scopes,$broadcast
- 将事件向下发送到所有子范围, -
$emit
-- dispatches the event upwards through the scope hierarchy.$emit
- 通过范围层次结构向上调度事件。
I don't know anything