javascript 如何在 AngularJS 中将消息从一个控制器发送到另一个控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17775285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:34:50  来源:igfitidea点击:

How can I send a message from one controller to another in AngularJS?

javascriptangularjs

提问by Eliran Malka

I have the following set up:

我有以下设置:

stApp.controller('AdminTableController', ['$rootScope', '$scope', 'gridService', 
            function ($rootScope, $scope, gridService) {
    $scope.$watch('tableData.$pristine', function (newValue) {
        $rootScope.broadcast("tableDataUpdated", {
            state: page.$pristine
        });
    });
}])

stApp.controller('AdminGridController', ['$rootScope', '$scope', 'gridService',
            function ($rootScope, $scope, gridService) {
    $rootScope.on("tableDataUpdated", function (args) {
        //args.state would have the state.
        alert(args.state);
    });
}])

When I run this code I am getting a message:

当我运行此代码时,我收到一条消息:

Object #<Object> has no method 'on'

Note that I tried this with both $rootScope.on and $scope.on

请注意,我尝试了 $rootScope.on 和 $scope.on

回答by Eliran Malka

You must have meant $broadcastand $on(rather than broadcastand on), that is:

您的意思一定是$broadcastand $on(而不是broadcastand on),即:

$rootScope.$broadcast("tableDataUpdated", { state: page.$pristine });
// ...
$rootScope.$on("tableDataUpdated", function (args) {
// ...

It's worth noting that $broadcastis used to delegate events to child or sibling scopes, whereas $emitwill bubble events upwards to the scope's parents, hence;

值得注意的是,$broadcast用于将事件委托给子作用域或兄弟作用域,而$emit将事件向上冒泡到作用域的父作用域,因此;

When choosing $broadcast(and not $emit), one should either inject the root scope for tying the $on(as you nicely did) or call $onon the receiver's isolated scope, be it a child scope of the dispatcher.

在选择$broadcast(而不是$emit)时,应该注入根作用域以绑定$on(正如您所做的那样)或调用$on接收器的隔离作用域,无论是调度程序的子作用域。

See this postfor elaboration on $emitand $broadcast.

有关和 的详细说明,请参阅此帖子$emit$broadcast

回答by Jake McGuire

If the event listener is in a child scope: $scope.$broadcast('MyEvent', args);

如果事件侦听器在子范围内$scope.$broadcast('MyEvent', args);

If the event listener is in a parent scope: $scope.$emit('MyEvent', args);

如果事件侦听器在父作用域中$scope.$emit('MyEvent', args);

You could avoid any confusion by using $broadcast on $rootScope, since $rootScope is the parent of all scopes in an Angular application: $rootScope.$broadcast('MyEvent', args);

您可以通过在 $rootScope 上使用 $broadcast 来避免任何混淆,因为 $rootScope 是 Angular 应用程序中所有作用域的父级: $rootScope.$broadcast('MyEvent', args);

Whether you use $emit or $broadcast, the receiving controller listens with $scope.$on('MyEvent', function() {});

无论您使用 $emit 还是 $broadcast,接收控制器都会监听 $scope.$on('MyEvent', function() {});

回答by rajasaur

 $rootScope.on("tableDataUpdated", function (args) {
        //args.state would have the state.
        alert(args.state);
    });

should be

应该

 $rootScope.$on("tableDataUpdated", function (args) {
        //args.state would have the state.
        alert(args.state);
    });

回答by 0xAX

$scope.$emit('MyEvent', args);

from first controller, and receive in second controller:

从第一个控制器,并在第二个控制器接收:

$scope.$on('MyEvent', function() {});