$ rootScope.$ emit不工作

red*_*rom 3 javascript emit angularjs

我正在尝试使用以下方法将AngularJS中的事件发送到第二个控制器:

第一控制员:

    $scope.checkThemeContent = function(theme) {
    console.log("Trying to get cards for theme id: " +theme.id);
    console.log(theme);

    $scope.selectedTheme = theme;

    if(theme.count_of_cards >0) {
        //$scope.$emit('detailDisplayed', {});
        $scope.displayedTemplate = 'detail';
        $rootScope.$emit('detailDisplayed', {});
    } else {
        $scope.displayedTemplate = 'empty';
    }
};
Run Code Online (Sandbox Code Playgroud)

第二控制器:

 $rootScope.$on('detailDisplayed', function(event, args) {
        alert('Received');
        $scope.initDataGrid();
    });
Run Code Online (Sandbox Code Playgroud)

但事件不会被触发.

我试图在反方向使用范围,它的工作原理.

请问哪里有问题?

我按照本教程:

http://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/

谢谢你的帮助.

Som*_*ens 9

我试图在反方向使用范围,它的工作原理.

听起来像是你的代码被构造成在$on附加处理程序之前发出事件的位置$rootScope.确保在发出事件之前已实例化第二个控制器.