0

Angular の を使用しようとして$broadcast、テストして文字列 'hi' を使用し、兄弟コントローラーでそれを監視すると、次のように正常に動作します。

//first controller
app.controller('colourKeyCtrl', colourKeyCtrl);
function colourKeyCtrl($scope, $timeout, patents, patentPhasesService) {
    vm.$onInit = function() {
       $scope.$broadcast('hi');
    }
}

//second controller
app.controller('graphDonutCtrl', graphDonutCtrl);
function graphDonutCtrl($scope, patents, patentPhasesService, $timeout) {
    $scope.$on('hi', function(event, opt){
       alert('hello there')
    })    
}

文字列を などの別のものに変更するとすぐに、2 番目のコントローラーでメソッドphaseChangeを呼び出すことができません。$on理由がわからない。メソッドをラップしようとし$broadcastました$timeoutが、問題は解決しませんでした。

質問

間違った方法で使用$broadcastしていますか、それとも構文が間違っていますか?

.state('dashboard', {
    url: '/dashboard',
    views: {
        '@': {
            templateUrl: 'p3sweb/app/components/dashboard/views/dashboard.htm',
            controller: 'dashboardCtrl',
            controllerAs: '$ctrl'
        },
        'colourkeywidget@dashboard': {
            templateUrl: 'p3sweb/app/components/dashboard/views/ui-views/colour-key-widget.htm',
            controller: 'colourKeyCtrl',
            controllerAs: '$ctrl'                
        },
        'graphdonutwidget@dashboard': {
            controller: 'graphDonutCtrl',
            controllerAs: '$ctrl',                
            templateUrl: 'p3sweb/app/components/dashboard/views/ui-views/graph-donut-widget.htm',         
        }
    }
})
4

1 に答える 1

1

$broadcast現在のスコープから始まり、子スコープまで下がります。

$emit現在のスコープから開始し、スコープ チェーンを上っていきます。

兄弟スコープに移動するイベントディスパッチメソッドはありません$emit。親まで行き、そこでイベントを処理して元に戻す必要があり$broadcastます。

于 2018-03-06T15:00:42.430 に答える