尝试使用 jQuery File Upload 插件和该指令的要点创建文件上传指令https://gist.github.com/thoughtpalette/4726114
我需要将 profile-ctrl.js 中对象的 id 传递到上传表单的提交功能中。
我的指令位于控制器之外,我无法将对象设置为 $rootScope 以在指令中捕获/传递它。
url: in fileupload 是魔法发生的地方。通过 api 调用我的控制器并传递文件信息,并希望在 profile-ctrl.js 中的商家对象中传递 vendorId。$scope.merchant
不幸的是,由于项目结构,我无法在小提琴中发帖
我的指令:
//jqueryFileUpload-Plugin
//https://github.com/blueimp/jQuery-File-Upload
define(['jquery',
'angular',
'core',
'source/core/helpers/urlHelper.js'],
function ($, angular, core, urlHelper) {
"use strict";
return [ function ($compile) {
return {
restrict:'E',
scope: {
merchant: '=ngModel'
},
compile:function(el,attrs, scope){
var compiler = this,
elem = el,
vendorId = scope.merchant.id;
instanceFn = function() {
var currentScope = this,
fileInputDiv = $(' <span class="btn btn-success fileinput-button">'+
'<i class="icon-plus icon-white"></i>'+
'<span>Upload Logo</span>'+
'<input type="file" name="files[]" single>'+
'</span>').appendTo(elem),
fileInput = fileInputDiv.find('input'),
fileList = $('<div class="UploaderList">'+
'<table>'+
'<tr>'+
'<td>File to Upload</td>'+
'</tr>'+
'</table>'+
'</div>').appendTo(elem),
button = $('<button class="btn">Submit</button>').appendTo(elem);
button.hide();
$('<div class="uploader">').appendTo(elem);
$('</div>').appendTo(elem);
fileInput.fileupload({
url:urlHelper.vendor.uploadLogo(vendorId),
dataType: 'json',
add:function (e, data) {
button.show();
// this will add a handler which will submit this specific file
button.bind('click.uploadsubmit', function(){
data.submit();
});
$.each(data.files, function (index, file) {
$("<tr><td>"+file.name+"</td><td></td><tr>").appendTo(fileList.find('table:first'));
});
},
// for each file
done: function (e, data) {
button.hide();
var result = "",
r = data.result,
file = data.files[0]; // we only support single file upload by now
// error
// CHANGE THIS PART FOR YOUR REQUIREMENTS (I have a json repsonse)
if(r.success !== undefined && r.success === false){
result = "<span class='error'>Unknown error</span>";
if(r.errors !== undefined && r.errors.length > 0 && r.errors[0].message !== undefined)
{
result = "<span class='error'>"+r.errors[0].message+"</span>";
}
}
else{
result = "<span class='success'>OK</span>";
}
$("td:contains('"+file.name+"')").next().html(result);
},
progressall:function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
},
// called only once... (wen submit button is clicked)
start:function(e){
// we start the upload, so we do also cleanup jobs right now:
button.unbind('click.uploadsubmit'); // importan - remove all event handlers
button.hide();
fileInputDiv.hide();
}
});
};
return instanceFn;
}
};
}];
});
通过 html 的指令调用:
<jquery-File-Upload ng-model="merchant"></jquery-File-Upload>
所以现在商家在指令中返回未定义。有人可以帮我把它传进去吗?