2013-07-22 19 views
10

đây là cách tôi sử dụng tập tin jquery góc uploadcách sử dụng phiên bản góc tải lên tệp jquery?

var album = angular.module('album', ['restangular', 'blueimp.fileupload']), 

.controller('somecontroller',function($scope,){ 
    $scope.options = { 
     something 
    } 

}) 

tất cả tôi đã được thiết lập scope.options, thay đổi bộ điều khiển, và tất cả mọi thứ chỉ là kỳ diệu làm việc

thiết lập các tập tin tải lên jquery có vẻ khá dễ dàng, nhưng có điều gì đó thực sự gây nhầm lẫn cho tôi

làm cách nào tôi có thể gọi hàm gọi lại của tệp tải lên jquery. ví dụ, nếu các tập tin được tải lên thành công, tôi muốn cập nhật ui bằng cách gọi hàm fileuploaddone, nó gây nhầm lẫn cho tôi bởi vì không có tập tin được thêm vào trong bộ điều khiển của tôi.

Tôi mới đến angularJS, hãy giúp tôi hiểu được công việc của tập tin jquery góc upload

Trả lời

25

các blueimp.fileupload sử dụng sự kiện được bắn qua $ phát ra thông báo phạm vi cha mẹ:

   on([ 
       'fileuploadadd', 
       'fileuploadsubmit', 
       'fileuploadsend', 
       'fileuploaddone', 
       'fileuploadfail', 
       'fileuploadalways', 
       'fileuploadprogress', 
       'fileuploadprogressall', 
       'fileuploadstart', 
       'fileuploadstop', 
       'fileuploadchange', 
       'fileuploadpaste', 
       'fileuploaddrop', 
       'fileuploaddragover', 
       'fileuploadchunksend', 
       'fileuploadchunkdone', 
       'fileuploadchunkfail', 
       'fileuploadchunkalways', 
       'fileuploadprocessstart', 
       'fileuploadprocess', 
       'fileuploadprocessdone', 
       'fileuploadprocessfail', 
       'fileuploadprocessalways', 
       'fileuploadprocessstop' 
      ].join(' '), function (e, data) { 
       if ($scope.$emit(e.type, data).defaultPrevented) { 
        e.preventDefault(); 
       } 
      }) 

Điều đó có nghĩa rằng bạn chỉ có thể nghe thêm một sự kiện trong một bộ điều khiển phạm vi phụ huynh, ví dụ:

$scope.$on('fileuploadprocessdone', function(event, files){ 
    $.each(files, function (index, file) { 
     //do what you want 
    }); 
}); 

bạn cũng có thể ghi đè lên handleResponse mặc định trong giai đoạn cấu hình của bạn, ví dụ:

angular.module('myApp', ['blueimp.fileupload']). 
.config(['fileUploadProvider', function (fileUploadProvider){ 
    fileUploadProvider.defaults.handleResponse = function (e,data){ 
     var files = data.result && data.result.files; 
     if (files) { 
      data.scope().replace(data.files, files); 
      // do what you want... 
     } else if (data.errorThrown || data.textStatus === 'error') { 
      data.files[0].error = data.errorThrown || 
      data.textStatus; 
     } 
    };  
}]); 
+0

+1 nhưng đáng chú ý là nếu bạn sử dụng $ scope ($) của angular để ràng buộc (v1.1.5) như trái ngược với $ element.on của jQuery() Hiện KHÔNG chấp nhận danh sách các tên sự kiện như phiên bản jquery. IOW, bạn KHÔNG THỂ làm điều này $ scope. $ On (['idontwork', 'nor-me']. Join ('')], function() {}). Bắt tôi đi. Xem https://groups.google.com/forum/#!topic/angular/Ilv1uPOTxgY – cirrus

+1

lưu ý rằng tham số thứ hai từ gọi lại sự kiện là cá thể tải lên tệp thay vì một mảng có tệp. sử dụng 'function (event, fileuploader) {console.log (fileuploader.files); }) 'thay thế. – paul

+0

Và còn fileuploadDESTROY thì sao? – Ulrira