0

I have the following html code

<input type="file" ng-model="Import" file-reader="fileContent" ng-change="getList();"/>

As shown above, i am trying to load a text file and get it contents on ng-change.

Here is the directive 'fileReader'

ngUtilityMod.directive('fileReader', function () {
return {
    scope: {
        fileReader: "="
    },
    link: function (scope, element) {
        $(element).on('change', function (changeEvent) {
            var r = new FileReader();
            var CSVfile;
            var checkCSV = changeEvent.target.files[0].name;
            checkCSV = checkCSV.slice(-4);
            if (checkCSV === '.csv') {
                CSVfile = true;
            }
            else { CSVfile = false; }
            if (changeEvent.target.files[0].type === 'text/plain' || CSVfile === true) {
            var files = changeEvent.target.files;
            if (files.length) {

                r.onload = function (e) {
                    var contents = e.target.result;
                    scope.$apply(function () {
                        scope.fileReader = contents;
                    });
                };
                r.readAsText(files[0]);
            }
            }
            else {
                'data';
            }


        });
    }
};

});

When ng-change is triggered, i should be able to get to below function. The issue here is that ng-change is not getting triggered at all. If i use ng-click or any other angularjs events, it is able to detect. Can someone please let me know why i am unable to trigger ng-change here

    $scope.getList = function () {
    $scope.getImport = function (fileContent) {
    } // function to load CSV data during import feature.

    if (angular.isDefined($scope.fileContent)) {
        $scope.Import = $scope.fileContent;
        console.log($scope.params);
    }

};
Rihana
  • 403
  • 2
  • 7
  • 14
  • Can someone please let me know the reason for downvoting ? was it not clear or was it to trivial to ask "? – Rihana Aug 23 '17 at 14:52
  • Ng-change doesn't work on input type="file". I would suggest you use angular-file-upload or have a look here: https://stackoverflow.com/questions/37966899/call-a-function-from-inputs-onchange – BelgoCanadian Aug 24 '17 at 04:37

0 Answers0