javascript $scope.$on 不触发 $rootScope.$broadcast 在角度服务中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26728072/
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-28 06:24:07  来源:igfitidea点击:

$scope.$on not triggering after $rootScope.$broadcast in angular service

javascriptangularjs

提问by Saurabh Palatkar

This may be the repeated question but the workaround I found for this issue is not working in my case thats why I am posting the question.

这可能是重复的问题,但我为这个问题找到的解决方法在我的情况下不起作用,这就是我发布问题的原因。

I've following service:

我有以下服务:

appRoot.service('MyService', function($rootScope) {
    var Messenger = {
        Temp: "",
        TempId:"",
        tempMethod: function(Id) {
            TempId = Id;
            $rootScope.$broadcast('FirstCtrlMethod');
        }
    };
    return Messenger ;
});

In second controller:

第二个控制器中:

appRoot.controller('SecondCtrl', function ($scope, $location, MyResource, NotificationService, MyService) {
 $scope.invokeFirstCtrl= function() {
        var Id = '2';
        MyService.tempMethod(Id);
});

In first controller:

第一个控制器中:

appRoot.controller('FirstCtrl', function ($scope, $compile, $filter, $modal, $sce, $location, NotificationService, MyService) {
$scope.$on('FirstCtrlMethod', function () {        
        alert('I am from frist controller');
    });
});

Problem:The line "$rootScope.$broadcast('FirstCtrlMethod');"is executing as expected but it is not causing to fire event "$scope.$on('FirstCtrlMethod', function () {.."in the first controller. I've used the differenct services in many places in my app in the same way and they are workig fine, I am not understanding why it is not working here.

问题:线“$ $ rootScope广播( 'FirstCtrlMethod');”。正在按预期执行,但它不会导致在第一个控制器中触发事件“$scope.$on('FirstCtrlMethod', function () {..”。我在我的应用程序的许多地方使用了不同的服务同样的方式,他们工作正常,我不明白为什么它在这里不起作用。

回答by harishr

putting comment as an answer...

将评论作为答案...

I guess the other controller which is supposed to receive the event is not yet instatiated when you are $broadcasting the event.

我猜当您 $broadcasting 事件时,应该接收事件的另一个控制器尚未设置。

Please try instantiating the other controller

请尝试实例化另一个控制器

回答by sylwester

Please see below working example

请参阅下面的工作示例

var app = angular.module('app', []);

app.service('MyService', function($rootScope) {
  var Messenger = {
    Temp: "",
    TempId: "",
    tempMethod: function(Id) {
      TempId = Id;
      $rootScope.$broadcast('FirstCtrlMethod');
    }
  };
  return Messenger;
});

app.controller('homeCtrl', function($scope, MyService) {

  $scope.invokeFirstCtrl = function() {
    var Id = '2';
    MyService.tempMethod(Id);

  };
});

app.controller('FirstCtrl', function($scope) {

  $scope.$on('FirstCtrlMethod', function() {
    alert('I am from frist controller');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="homeCtrl">

    <button ng-click="invokeFirstCtrl()">Invoke</button>
  </div>

  <div ng-controller="FirstCtrl">


  </div>
</div>