In this post I will share the code how to detect the change event for HTML file input. We are supporting an old application written with AngularJS, and encountered this problem. If you are using default ng-change
attribute, it will not work in AngularJS because of no binding support for file input control. I will show you I solved this problem in my case. I made a custom directive to listen for file input changes which enables us to invoke the custom event handler function. Lets start coding this solution:
Here is the custom directive fileOnChange
definition:
myApp.directive('fileOnChange', function () { return { restrict: 'A', link: function (scope, element, attrs) { var onChangeHandler = scope.$eval(attrs.fileOnChange); element.bind('change', onChangeHandler); } }; });
Here is the HTML for file input showing how to bind this directive to listen for file changes.
<input type="file" file-on-change="onFileChange"> </input>
Now the final part of this task, write the handler function to do the actual work required on file change. Within the target controller, I write this handler function, in this example it is only showing the selected file name in alert box.
$scope.onFileChange = function (event) { var filename = event.target.files[0].name; alert('File name: ' + filename); };
Innovative think. Thank You for your great post. I need more info to learn so kindly update it.
ReplyDeleteAngular JS Online training
Angular JS training in Hyderabad
Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
ReplyDeleteangular js online training
best angular js online training
top angular js online training
I really liked your blog article. Really thank you! Really Cool.
ReplyDeletepython training
angular js training
selenium trainings
ReplyDeleteReally awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
Angularjs Development Company in India
This comment has been removed by the author.
ReplyDelete