Директива для директивной связи с использованием вещания angularjs

У меня есть две директивы, и мне нужно передать значение из одной директивы после запроса post в другую директиву.

Первая директива выглядит так

var fileUpload = angular.module('fileUploadDirective',[]);
fileUpload.directive('fileUpload', function () {
        return {
            restrict: 'EA',
            templateUrl: 'app/views/fileUpload/file-upload.tpl.html',
            controller: fileUploadCtrl
        }
    });
fileUploadCtrl.$inject = ['$scope', '$rootScope', '$http','FileUploader'];
function fileUploadCtrl($scope, $rootScope, $http, FileUploader) {

    var vm =this
    vm.uploader.onBeforeUploadItem = function (item) {            
        $rootScope.$broadcast("NEW_EVENT",data); //send event
    }
}

Вторая директива выглядит так

var resultPage = angular.module('resultPageDirective',[]);
resultPage.directive('resultDirective',function(){
    return {
        restrict: 'EA',
        scope: {},
        replace: true,
        link: function($scope, element, attributes){
        },
        controller: function($scope,$rootScope,$attrs,$http){    
            $scope.junkFiles = ["abc","xyz"];
            $scope.$on("NEW_EVENT",function(event,data){ //listen to event
                   console.log(data);
            });

        },
        templateUrl: 'app/views/resultPage/resultPage.tpl.html'
    }
});

Второе директивное событие не прослушивается

1 ответ

Для прослушивания событий $scope вы должны использовать $on вместо on

$scope.$on("NEW_EVENT",function(event,data){ //listen to event
       console.log(data);
});
Другие вопросы по тегам