AngularJs: How to check for changes in file input fields?

前端 未结 15 1392
傲寒
傲寒 2020-11-22 09:06

I am new to angular. I am trying to read the uploaded file path from HTML \'file\' field whenever a \'change\' happens on this field. If i use \'onChange\' it works but when

15条回答
  •  春和景丽
    2020-11-22 09:34

    I recommend to create a directive

    
    
    app.directive('customOnChange', [function() {
            'use strict';
    
            return {
                restrict: "A",
    
                scope: {
                    handler: '&'
                },
                link: function(scope, element){
    
                    element.change(function(event){
                        scope.$apply(function(){
                            var params = {event: event, el: element};
                            scope.handler({params: params});
                        });
                    });
                }
    
            };
        }]);
    

    this directive can be used many times, it uses its own scope and doesn't depend on parent scope. You can also give some params to handler function. Handler function will be called with scope object, that was active when you changed the input. $apply updates your model each time the change event is called

提交回复
热议问题