Hi I am developing angularjs application. I have file upload module. Below is my html code.
<input type="file" file-modelsr="myFileID" name="fileupld" valid-files required />
On submitting form I am trying to get something like this below.
console.log(form2.fileupld.$valid);
This always gives me undefined. How can i check file has been uploaded or not on submitting the form?
I have used below directive.
myapp.directive('validFiles', function () {
return {
require: 'ngModel',
link: function (scope, el, attrs, ngModel) {
//change event is fired when file is selected
el.bind('change', function () {
debugger;
scope.$apply(function () {
ngModel.$setViewValue(el.val());
ngModel.$render();
})
})
}
}
})
I used below directive to upload files.
myapp.directive('fileModelsr', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var model = $parse(attrs.fileModelsr);
var modelSetter = model.assign;
element.bind('change', function () {
scope.$apply(function () {
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
How can I apply required file validation in AngularJS?