HTML文件输入Angularjs选择相同的文件仅触发一个

时间:2021-07-26 19:40:45

I've made a Angular directive to warp the a file upload input.

我制作了一个Angular指令来扭曲文件上传输入。

directive('ngFile', function () {
    return {
        link: function (scope, element, attrs) {
            var button = element.find('button[data-fileInputButton]');
            var inputElement = element.find('input[type="file"]');        

            var updateModel = function (evt) {
                scope.$apply(function () {
                    scope.files = evt.target.files;
                    if (scope.startUpload) {
                        scope.startUpload();
                    }

                    evtl.target.files = [];
                });
            }

            inputElement.bind('change', updateModel);

            button.click(function () {
                var input = element.find('input[type="file"]');
                input.click();
            });
        }
    } 
});

When I click and select a file, everything is OK. However, when I select the file again, the event is not fired. It is only fired when I select another file. Why is this happening?

当我单击并选择一个文件时,一切正常。但是,当我再次选择文件时,不会触发该事件。它只在我选择另一个文件时被触发。为什么会这样?

1 个解决方案

#1


The change is listening to the value and not to the files.

更改是侦听值而不是文件。

you need to do:

你需要做的:

evt.target.value = "";

Good luck :)

祝好运 :)

#1


The change is listening to the value and not to the files.

更改是侦听值而不是文件。

you need to do:

你需要做的:

evt.target.value = "";

Good luck :)

祝好运 :)