-2

2 つのディレクティブがあり、ポスト リクエストの後に 1 つのディレクティブから別のディレクティブに値を渡す必要があります。

最初のディレクティブは次のようになります

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
    }
}

2 番目のディレクティブは次のようになります。

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

2 番目のディレクティブ イベントはリッスンされません

4

1 に答える 1

0

$scope のイベント リスニングには、$on代わりに使用する必要があります。on

$scope.$on("NEW_EVENT",function(event,data){ //listen to event
       console.log(data);
});
于 2016-08-11T01:32:07.640 に答える