forked from ghostbar/angular-file-model
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-file-model.js
More file actions
36 lines (31 loc) · 868 Bytes
/
angular-file-model.js
File metadata and controls
36 lines (31 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//
// angular-file-model
// ==================
//
// Directive that makes the inputs with type `file` to be
// available in the `$scope` and be assigned to a model.
//
(function () {
'use strict';
angular.module('file-model', [])
.directive('fileModel', function () {
return {
require: 'ngModel',
restrict: 'A',
link: function($scope, element, attrs, ngModel) {
var checkIsValid = function(){
ngModel.$setValidity('validFile', element.val() !=='');
};
checkIsValid();
element.bind('change', function () {
$scope.$apply(function () {
checkIsValid();
ngModel.$setViewValue(attrs.multiple ? element[0].files : element[0].files[0]);
ngModel.$render();
});
});
}
};
}
);
})();