Angularjs 上载多个文件,其中显示文件大小、文件名等属性,以及删除选项和取消等

Angularjs 上载多个文件,其中显示文件大小、文件名等属性,以及删除选项和取消等,angularjs,Angularjs,我遇到了这个需求,在这个需求中,我需要上传多个文件,并显示其他属性,如显示文件名、文件大小,以及当有人单击“删除”选项时,特定的文件也应该被删除 我想把这个问题贴出来作为其他人的参考,只是为了帮助某人实现这个目标,请按照下面的步骤进行 var app=angular.module("MyApp",[]); app.directive('fileUpload', function () { return {

我遇到了这个需求,在这个需求中,我需要上传多个文件,并显示其他属性,如显示文件名、文件大小,以及当有人单击“删除”选项时,特定的文件也应该被删除


我想把这个问题贴出来作为其他人的参考,只是为了帮助某人实现这个目标,请按照下面的步骤进行

 var app=angular.module("MyApp",[]);
            app.directive('fileUpload', function () {
                return {
                    scope: true,        //create a new scope
                    link: function (scope, el, attrs) {
                        el.bind('change', function (event) {
                            var files = event.target.files;
                            //iterate files since 'multiple' may be specified on the element
                            for (var i = 0;i<files.length;i++) {
                                //emit event upward
                                scope.$emit("fileSelected", { file: files[i] });
                            }                                       
                        });
                    }
                };
            });

            app.controller("MyCtrl",function($scope, $http) {



                //a simple model to bind to and send to the server
                $scope.model = {
                    name: "",
                    comments: ""
                };

                //an array of files selected
                $scope.files = [];


                //listen for the file selected event
                $scope.$on("fileSelected", function (event, args) {
                    $scope.$apply(function () {            
                        //add the file object to the scope's files collection
                        $scope.files.push(args.file);
                    });
                });



                $scope.remove=function(file,files){
                console.log("size: "+($scope.files).length);
                if(files.indexOf(file)!=-1){
            files.splice(files.indexOf(file),1);
                }
                }

                $scope.validate=function(size,file,files){
                var status=false;
                console.log("size @@: "+($scope.files).length);
                if(size >2048){

            if(files.indexOf(file)!=-1){
            files.splice(files.indexOf(file),1);
            return true;
                }
                }

                if(files.length>5){

                if(files.indexOf(file)!=-1){
            files.splice(files.indexOf(file),1);
            return false;
                }
                }

                }

                $scope.cancel=function(files){
                console.log("Cancelled");
                files.length=0;
                }
            });
var-app=angular.module(“MyApp”,[]);
应用程序指令('fileUpload',函数(){
返回{
scope:true,//创建一个新的作用域
链接:功能(范围、el、属性){
el.bind(‘变更’、功能(事件){
var files=event.target.files;
//迭代文件,因为可以在元素上指定“多个”
对于(var i=0;i