Skip to content

Instantly share code, notes, and snippets.

@MrRaindrop
Last active August 29, 2015 14:01
Show Gist options
  • Save MrRaindrop/ddbe109032160af03cf8 to your computer and use it in GitHub Desktop.
Save MrRaindrop/ddbe109032160af03cf8 to your computer and use it in GitHub Desktop.
angular自定义directive [filelistread]: 多文件上传,解决ngModel读不了上传文件的问题,并补充limit数量限制
/**
* usage:
* <form name="form">
* <input type="file" multiple name="uploads" filelistread limit="8" ng-model="uploads" />
* <label ng-show="form.uploads.$error.limit">上传文件数量限制最多为8个</label>
* </form>
*/
angular.module('testapp', []).directive("filelistread", function() {
return {
require: 'ngModel',
scope: {
limit: '@',
// filelistread: '='
// ngModel: '='
},
link: function(scope, elem, attr, ctrl) {
elem.bind('change', function(changeEvent) {
scope.$apply(function() {
ctrl.$setViewValue(changeEvent.target.files);
if (ctrl.$viewValue.length > scope.limit) {
ctrl.$setValidity('limit', false);
} else {
ctrl.$setValidity('limit', true);
}
});
});
}
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment