-2

두 개의 지시문이 있으며 한 요청에서 다른 지시문으로 값을 전달해야합니다.브로드 캐스트 앵글을 사용하는 지시문 통신에 대한 지시문

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

두 번째 지시는 두 번째 지시 이벤트가 $ 범위의 수신 이벤트의

+0

그래서 문제 또는 질문은 무엇입니까? 좋은 시간이 될 수도 있습니다 [질문] – charlietfl

+0

두 번째 지시문이 작동하지 않습니다. 들을 수 없습니다. – krs8888

+0

두 번째 지시문에 구문 오류가 있습니다 (누락 된 중괄호 누락).하지만 게시하기 전에 코드를 잘못 편집 한 적이 있습니까? 그렇지 않으면 콘솔에 오류가 발생하여 지시문이 컴파일되지 않습니다. $ rootScope에서 듣기를 시도 했습니까? * $ 범위에서 작동해야하지만 때때로 문제가 있습니다. –

답변

0

을 경청하지 않는

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

모양처럼

첫 번째 지침 보이는, 당신은 $on를 사용해야합니다 대신 on

$scope.$on("NEW_EVENT",function(event,data){ //listen to event 
     console.log(data); 
}); 
+0

아니, 여전히 작동하지 않습니다 – krs8888

+0

안녕하세요 당신은 첫 번째 지시문의 컨트롤러에서 'VM을'정의되지 않은 오류가나요? 그건 내가 생각하기에'controllerAs '가 사용 된 것이 아닙니다. 최소한'var vm = this '가 필요합니다. – MMhunter

+0

오류가 없습니다. 내 코드는 var vm = this – krs8888

관련 문제