查看输入类型文件与多个使用Angularjs选择的文件
我想要预览从多个输入类型文件中选择的图像。为此,我想要列出文件路径并使用这些路径,我将预览图像。查看输入类型文件与多个使用Angularjs选择的文件
但在这里我img
标签使用ng-src
,这些路径上ng-src
设置和我也想根据使用ng-repeat
文件选择创建img
标签。
假设我将在input type=file
上选择5个文件,然后创建5 img
标记。假设我将文件从5更改为2,则删除全部5 img
标记,并创建2 img
标记。
我不知道ng-repeat
是否是根据文件选择创建img
标签的正确选项?
下面这是我myfileupload指令
App.directive('myFileUpload', function (fileService) {
return {link: function (scope, element, attrs) {
element.bind('change', function() {
var index;
var index_file = 0;
for (index = 0; index < element[0].files.length; index++) {
var file=element[0].files[index];
fileService.setFile(element[0].files[index], index_file, attrs.myFileUpload);
index_file++;
var src={};
/*src["src"]=element[0].files[index];*/
fileService.setFilePath(element[0].files[index], index_file, attrs.myFileUpload);
console.log(pathss);
path.push(src);
}
index_file = 0;
});
}
};
});
这是MYS服务
App.service('fileService', function() {
var fileService = {};
fileService.getFile = function (name) {
return file[name];
};
fileService.setFile = function (newFile, index, name) {
if (index === 0 && file[name] === undefined)
/*file[name] = [];*/
console.log(file);
file[name][index] = newFile;
/*console.log(name);*/
};
fileService.getFilePath = function (name) {
return filepath[name];
};
fileService.setFilePath = function (newFile, index, name) {
if (index === 0 && file[name] === undefined)
/*file[name] = [];*/
console.log(file);
filepath[name][index] = newFile.val;
/*console.log(name);*/
};
return fileService;
})
控制器我是如何创建一个输入类型的文件
$scope.add=function(){
var name="descimg["+currentg1+"]";
var pathname="imgfile["+currentg1+"]";
file[name] = [];
var $div = angular.element("<div><label id='desccodeL["+currentg1+"]' for='desccodeL["+currentg1+"]''>Code "+currentg1+"</label><textarea rows='3' cols='6' id='desccode["+currentg1+"]' name='desccode["+currentg1+"]' ng-model='blog.desccode["+currentg1+"]''></textarea></div><div><label id='descimgL["+currentg1+"]' for='descimgL["+currentg1+"]'>Image "+currentg1+"</label><input type='file' id='descimg["+currentg1+"]' class='file' name='descimg["+currentg1+"]' my-file-upload='descimg["+currentg1+"]' multiple/></div>");
var e=document.getElementById("outerdiv");
var $div2=angular.element("<div ng repeat='path in "+fileService.getFilePath("descimg["+currentg1+"]"); +"'><img ng-src=''/></div></div >);
var e2=document.getElementById("displayblog");
angular.element(e).append($div).injector().invoke(function($compile) {
var scope = angular.element($div).scope();
$compile($div)(scope);
});
angular.element(e2).append($div2).injector().invoke(function($compile) {
var scope = angular.element($div2).scope();
$compile($div2)(scope);
});
currentg1++;
};
文件元素是动态创建调用add函数后,当我设置myservice.setFilePath它给newFile.va中的错误我!
好吧,我没有时间重写代码,但这里有一些东西来看看:
1)在你的服务,我不认为filepath
是不断定义,这可能就是你'收到错误。
2)我不认为这句法将工作:filepath[name][index]
也许你打算用点符号,就像这样: filepath.name[index]
3)在你的控制器,你有ISN字符串关闭,导致控制器中的其余代码被解释为一个字符串。
4)而不是控制器中的所有HTML字符串,检查角度的数据绑定功能。您应该能够在模板中创建所有这些HTML元素,并使用角度指令或数据绑定来使模板动态化。例如:
<div>
<label id='desccodeL{{currentg1}}'for='desccodeL{{currentg1}}'>
Code {{currentg1}}
</label>
</div>
1)因为我使用filepath [name] [index]作为熟悉的文件[名称] [索引],其中file是一个数组,它将所有文件从选定元素存储在文件[元素名称] [元素索引] 。类似的文件路径是存储名称和索引文件路径的数组。直到我使用文件[名称] [索引]上传服务器上的文件在每个请求我得到文件形式文件[名称]并以多部分数据的形式发送到服务器。 2)遗憾地说,我没有添加声明为全局的文件和文件路径的声明。 –
你能发表一些代码吗?这听起来像你在正确的轨道上,但看到你的模板和控制器将有助于确保。 – jordajm
@jordajm我改变了我的文章,并包括我使用的一些代码。我认为它可以帮助你理解我的问题。 –