1

I have following code.

directive

.directive('area', function () {
    return {
        link: function (scope, el, attrs) {
            var fileReader = new FileReader();

            fileReader.onload = function (e) {
                scope.image = e.target.result;
                scope.$apply();
            }

            el.on('change', function () {
                fileReader.readAsDataURL(el[0].files[0]);
            });
        }
    };
});

controller

.controller('areaController',
    ['$scope', 
        function ($scope) {
            $scope.image = "";
        }
    ]
);

Somehow when I add a image to file input then I get following error

areaDirective.js:19 Uncaught TypeError: Cannot read property '0' of undefined

1 Answer 1

2

I guess you should modify it to

el.on('change', function (e) {
 var files = (e.srcElement || e.target).files
    fileReader.readAsDataURL(files[0]);
 });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.