Last active
August 29, 2015 14:01
-
-
Save MrRaindrop/e67542a1069faca281da to your computer and use it in GitHub Desktop.
angular自定义directive [filelistread]: 多文件上传,解决ngModel读不了上传文件的问题,并补充limit数量限制
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* multiple files upload | |
* 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: '@', | |
ngModel: '=' | |
}, | |
link: function(scope, elem, attr, ctrl) { | |
elem.bind('change', function(changeEvent) { | |
scope.$apply(function() { | |
scope.ngModel = changeEvent.target.files; | |
// ctrl.$setViewValue(changeEvent.target.files); | |
// scope.filelistread = changeEvent.target.files; | |
if (scope.ngModel.length > scope.limit) { | |
// 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