$ scope。$ $在$ rootScope之后没有触发。$在角度服务中广播 [英] $scope.$on not triggering after $rootScope.$broadcast in angular service

查看:93
本文介绍了$ scope。$ $在$ rootScope之后没有触发。$在角度服务中广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我关注服务

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

第二个控制器中

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

第一个控制器:

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

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

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.

推荐答案

将评论作为答案......

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

这篇关于$ scope。$ $在$ rootScope之后没有触发。$在角度服务中广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆