gpt4 book ai didi

javascript - 清除 ng-repeat 中的文件输入

转载 作者:行者123 更新时间:2023-12-03 06:07:50 25 4
gpt4 key购买 nike

我有一组文件输入是通过 ng-repeat 从数组生成的。我希望能够在上传完成后清除输入。例如,上传的文件位于数组的索引 1 处。我不希望上传完成,我希望清除与该索引 (1) 关联的文件输入。我一开始尝试绑定(bind)到 ng-model 但没有成功。我应该使用 $broadcast 吗?有没有办法找到输入的值?

/// <reference path="../typings/tsd.d.ts" />
var app = angular.module("app", []);

app.controller("AppCtrl", function($scope, $q, FileFactory){
var ctrl = this;

ctrl.ff = FileFactory;

$scope.upload = function(){

var defer = $q.defer();

defer.promise(function(data){


})

var http = new XMLHttpRequest();

var formData = new FormData();

formData.append("file", FileFactory.uploads[$scope.index].file);
formData.append("file", FileFactory.uploads[$scope.index].fileName);

http.open("POST", "upload.cfc?method=upload");

http.addEventListener("load", function(){

if(this.status = 200){

// code to clear the input?

}

})

http.send(formData);

}

})

.factory('FileFactory', function(){

var uploads = [
{ file:null, fileName:null, },
{ file:null, fileName:null, },
{ file:null, fileName:null, }
];

return uploads;

})

.directive('fileUpload', function(FileFactory){

return {
scope:{
fileUpload:"=",
index:"@"
},
restrict:"A",
link:function(scope, element, attr, ctrl){

element.bind('change', function(e){

FileFactory.uploads[scope.index].file = e.target.files[0];
FileFactory.uploas[scope.index].fileName = e.target.files[0].name;

})

}
}

})


document.addEventListener("DOMContentLoaded", function(){

angular.bootstrap(document.querySelector('html'), ['app']);

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="bower_components/angular/angular.min.js"></script>
<script src="ts/app.js"></script>
</head>
<body>
<div ng-controller="AppCtrl as app">
<div class="repeat" ng-repeat="item in app.ff track by $index">
<input type="file" file-upload index="{{$index}}">
<input type="button" ng-click="upload($index)" value="upload">
</div>
</div>
</body>
</html>

最佳答案

在您的上传函数中,您应该获取在 html 中传递的索引,如下所示:

$scope.upload = function(index){

...

formData.append("file", FileFactory.uploads[index].file);
formData.append("file", FileFactory.uploads[index].fileName);

}

关于javascript - 清除 ng-repeat 中的文件输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39456627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com