Is there a way to trigger ng-change when a change occurs with this file directive?
I have implemented a custom file directive for this purpose.
The Directive:
app.directive('ngFileModel', ['$parse', function($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.ngFileModel);
var isMultiple = attrs.multiple;
var modelSetter = model.assign;
element.bind('change', function() {
var values = [];
angular.forEach(element[0].files, function(item) {
var value = {
// File Name
name: item.name,
//File Size
size: item.size,
//File URL to view
url: URL.createObjectURL(item),
// File Input Value
_file: item
};
values.push(value);
});
scope.$apply(function() {
if (isMultiple) {
modelSetter(scope, values);
} else {
modelSetter(scope, values[0]);
}
});
});
}
};
}]);
Any suggestions on how to implement ng-change without utilizing ng-model?
The HTML:
<input type="file" name="" ng-file-model="uploadThisImage" ng-change="onFileSelect($index)">