angularjs - Angular $rootScope.$broadcast() 事件在 Controller 中被捕获两次

标签 angularjs rootscope

单击按钮时广播事件:-

$scope.onButtonClick = function(){
    $rootScope.$broadcast('onButtonClick');
}

并在另一个 Controller 中捕获事件:-
$rootScope.$on('onButtonClick',function(event){
  alert("catched");
  console.log(event);
});

但即使它只发射了一次,它也捕获了两次。这是为什么?

最佳答案

事实证明,由于 ng-controller,多个 Controller 被实例化。 html 中的声明,也作为 ui-router 状态设置的一部分.

解决方案是删除其中一个声明。

关于angularjs - Angular $rootScope.$broadcast() 事件在 Controller 中被捕获两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25788032/

相关文章:

javascript - $scope.$on 在 $rootScope.$broadcast 之后不触发

javascript - $watch 阻止 rootScope 更改

angularjs - $rootscope.$emit/$broadcast 不会传递参数

javascript - 为什么不建议将 $rootScope 与函数一起使用?

javascript - Angular : switch HTML depending on screen resolution

javascript - 是否可以将函数结果与 angularJS 绑定(bind)?

angularjs - 如何从 angularjs Controller 连接到 postgres 数据库

javascript - angularjs 字符串和字符串树

javascript - rootscope 与 angularJs 范围

angularjs - 如何访问 angularjs 中的 $rootscope 变量?