本文介绍了条件“多重” < input type =" file">中的属性与AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个上传表单字段,可能允许或不允许用户选择多个文件。
I need an upload form field that may or may not allow the user to select more than one file.
我知道我可以这样做:
<input type="file" multiple ng-if="allow_multiple">
<input type="file" ng-if="!allow_multiple">
但是,我们知道这并不理想。
But, we know that is not ideal.
我试过
<input type="file" ng-multiple="allow_multiple">
但这不起作用。
似乎AngularJS ,但
From thw answers so far it really seems like there's no pretty way to do this.I opened this issue on their tracker, let's see what we get :-)https://github.com/angular/angular.js/issues/7714
推荐答案
最简单的方法是写你自己的 ngMultiple
指令。
The easiest way is to write your own ngMultiple
directive.
HTML(相关):
<label><input type="checkbox" ng-model="allowMultiple"> Allow multiple</label>
<hr>
<input
type="file"
class="hide"
accept="image/*"
ng-multiple="allowMultiple">
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');
}
});
}
};
});
这篇关于条件“多重” < input type =" file">中的属性与AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!