2015-01-07 38 views
7

我們剛剛將textangular升級到1.2.2,因爲這現在支持拖放。TextAngular fileDropHandler文檔

已經在textAngualrSetup中看到了defaultFileDropHandler,試圖找到任何文檔來支持這個或如何使用它。

defaultFileDropHandler: 
    /* istanbul ignore next: untestable image processing */ 
    function (file, insertAction) 
    { 
     debugger; 
     var reader = new FileReader(); 
     if(file.type.substring(0, 5) === 'image'){ 
      reader.onload = function() { 
       if(reader.result !== '') insertAction('insertImage', reader.result, true); 
      }; 

      reader.readAsDataURL(file); 
      return true; 
     } 
     return false; 
    } 

基本上,我們希望允許用戶拖動多個PDF文件,word文檔等,並上傳提交。

我們可以概率得到一個的方式加入的功能集成到defaultFileDropHandler的設置頁面中這方面的工作,

我們實行的Ta: -

<div text-angular data-ng-model="NoteText" ></div> 
然而

,有一個更清潔的方式來實現這一目標?

回答

9

抱歉缺少文檔!

基本上,當觸發HTML element.on("drop")事件時,會觸發defaultFileDropHandler。

通過textAngularSetup文件實現這一點很好,但會全局應用於所有實例。要僅爲textAngular的一個實例應用處理程序,請使用ta-file-drop屬性,該屬性應該是作用域上與defaultFileDropHandler簽名相同的函數的名稱。例如:

JS在控制器

$scope.dropHandler = function(file, insertAction){...}; 

HTML

<div text-angular data-ng-model="NoteText" ta-file-drop="dropHandler"></div> 
2

兩個偉大的答案,謝謝!

我只想把完整的代碼了覆蓋全球的情況下,因爲代碼只是一個片段...

app.config(function($provide) { 
    $provide.decorator('taOptions', [ '$delegate', function(taOptions) { 

     taOptions.defaultFileDropHandler = function(file, insertAction) { 
      // validation 
      if(file.type.substring(0, 5) !== "image") { 
       // add your own code here 
       alert("only images can be added"); 
       return; 
      } 
      if(file.size > 500000) { 
       // add your own code here 
       alert("file size cannot exceed 0.5MB"); 
       return; 
      } 

      // create a base64 string 
      var reader = new FileReader(); 
      reader.onload = function() { 
       reader.result && insertAction("insertImage", reader.result, true); 
      }; 

      reader.readAsDataURL(file); 
      return true; 
     }; 

     return taOptions; 
    }]); 
});