$ scope。$ $在$ rootScope之后不触发。$在角度服务中广播

时间:2021-03-27 13:32:25

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。$ broadcast('FirstCtrlMethod');”正在按预期执行,但它不会导致在第一个控制器中触发事件“$ scope。$ on('FirstCtrlMethod',function(){..”。我在我的应用程序的许多地方使用了差异服务同样的方式,他们工作得很好,我不明白为什么它不在这里工作。

2 个解决方案

#1


14  

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.

我猜想当你播放这个事件时,应该接收该事件的另一个控制器尚未实例化。

Please try instantiating the other controller

请尝试实例化其他控制器

#2


1  

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>

#1


14  

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.

我猜想当你播放这个事件时,应该接收该事件的另一个控制器尚未实例化。

Please try instantiating the other controller

请尝试实例化其他控制器

#2


1  

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>