Conditional “multiple” attribute in <input type=“file”> with AngularJS

前端 未结 8 2204
感情败类
感情败类 2021-01-11 18:02

I need an upload form field that may or may not allow the user to select more than one file.

I know I can do something like:



        
8条回答
  •  自闭症患者
    2021-01-11 18:35

    The easiest way is to write your own ngMultiple directive.

    HTML (relevant):

    
    

    JS:

    angular
        .module('app', [])
        .controller('appCtrl', function($scope) {
            $scope.allowMultiple = false;
        })
        .directive('ngMultiple', function () {
            return {
                restrict: 'A',
                scope: {
                    ngMultiple: '='
                },
                link: function (scope, element) {
                    var unwatch = scope.$watch('ngMultiple', function (newValue) {
                        if(newValue) {
                            element.attr('multiple', 'multiple');
                        } else {
                            element.removeAttr('multiple');
                        }
                    });
                }
            };
        });
    

    Plunker

提交回复
热议问题