3

次を使用して、AngularJSのイベントを2番目のコントローラーに発行しようとしています:

最初のコントローラー:

    $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';
    }
};

2 番目のコントローラー:

 $rootScope.$on('detailDisplayed', function(event, args) {
        alert('Received');
        $scope.initDataGrid();
    });

しかし、イベントはトリガーされません。

スコープを逆方向に使用しようとしましたが、動作します。

どこに問題がありますか?

私はこのチュートリアルに従いました:

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

助けてくれてありがとう。

4

2 に答える 2

3

覚えておいてください: エミットはプロトタイプ チェーンを上に行き、ブロードキャストは下に行きます。すでにトップにいる場合 (つまり$rootScope)、それ以上上に行くことはできません。$broadcastボード全体で発生するイベントを取得するために使用します。

$rootScope.$broadcast('detailDisplayed');

この記事をチェックしてください:

https://blog.safaribooksonline.com/2014/04/08/refactoring-angularjs-get-hands-filthy/

于 2014-11-05T20:53:21.860 に答える