2013-06-30 1 views
0

나는 이미지 목록을 표시하는 방법을 궁금합니다. 드래그 앤 드롭 부분에 대한 지침이 이미 있습니다. 하지만 내 응용 프로그램에서 "구문 분석"FileList 어떻게 부분에서 지시문에서 데이터를 전달합니까?이 작업은 컨트롤러에 있습니까?AngularJS : 이미지 목록을 표시하는 방법 (끌어서 놓기를 통해)

/** 
* Adds drag&drop functionality to a div 
* @Example: <div drop-target ng-class="{active:isHot}" class="dropzone">Drop here</div> 
*/ 
angular.module('myApp.directives'). 
    directive('dropTarget', function() { 
     return function ($scope, $element) { 

      // Drophandler passes a fileList to the controller 
      $element.bind('drop', function (evt) { 
       evt.stopPropagation(); 
       evt.preventDefault(); 

       // FileList should go to the controller/view 
       var fileList = evt.dataTransfer.files; 
       console.log(fileList); 

       $scope.$apply(function() { 
        $scope.imageList = fileList; 
       }); 

       return false; 
      }); 

      // DragOverHandler highlights the dropzone 
      $element.bind('dragover', function (evt) { 
       evt.stopPropagation(); 
       evt.preventDefault(); 

       $scope.isHot = true; 

       return evt.dataTransfer.dropEffect = "copy"; 
      }); 

      // DragOverHandler de-highlights the dropzone 
      $element.bind('dragleave', function (evt) { 
       evt.stopPropagation(); 
       evt.preventDefault(); 

       $scope.isHot = false; 
      }); 
     }; 
    }) 
+0

위의 코드에서 드래그 앤 드롭 한 파일 목록이 포함 된 변수를 알려주십시오. 그것은'fileList'입니까? – callmekatootie

+0

그래, 파일 목록 – fabian

+0

그럼 왜 '$ scope'에 묶지 않았 니? 지역 변수입니다 ... – callmekatootie

답변

0

그 기준으로 다음 줄을 복용, fileList 변수 드래그 및 삭제 된 파일이 포함되어 있다는 것입니다 가정은 :

// FileList should go to the controller/view 
var fileList = evt.dataTransfer.files; 

... 당신은 단순히 범위에 fileList 변수를 변경 지역 변수 대신 변수. 따라서 위의 코드는 당신은 위의 코드를 다음에 $scope.$apply() 기능이 필요하지 않습니다

$scope.fileList = evt.dataTransfer.files; 

을 읽을 것입니다. 대신, 파일은 이미 $scope.fileList에 있어야합니다. 그런 다음 drop-target 지시문이있는 뷰에서이 변수를 사용할 수 있습니다.

+0

고마워,하지만 난 드롭 대상 지시문 밖에서 fileList를 사용하고 싶습니다. – fabian

+0

@fabian 당신의 지시어가 새로운 범위를 만들지 않는다는 것을 고려하여 지시어 바깥에서 사용할 수 있어야한다고 생각합니다 ... – callmekatootie

관련 문제