1

I have data and I list the data using ng-repeat. Each row has a browse image. My problem is when I browse image, the image displays in all row which should not. It should display to the one row. Please Help.

Here is my fiddle link: http://jsfiddle.net/DharkRoses/m2qagzzk/2/

Sample codes in fiddle:

angular.module('test', []);

angular.module('test') .controller('UploadCtrl', function ($scope, $timeout) {

$scope.thumbnail = {
    dataUrl: 'adsfas'
};
$scope.fileReaderSupported = window.FileReader != null;
$scope.photoChanged = function (files) {
    if (files != null) {
        var file = files[0];
        if ($scope.fileReaderSupported && file.type.indexOf('image') > -1) {
            $timeout(function () {
                var fileReader = new FileReader();
                fileReader.readAsDataURL(file);
                fileReader.onload = function (e) {
                    $timeout(function () {
                        $scope.thumbnail.dataUrl = e.target.result;
                    });
                }
            });
        }
    }

1 Answer 1

3

You need to have separate dataUrl per iteration row. For example you can make use of $index variable:

<img ng-src="{{ thumbnail[$index].dataUrl }}" height="50px" />

and JS part:

$scope.thumbnail = {
    dataUrl: []
};

$scope.fileReaderSupported = window.FileReader != null;
$scope.photoChanged = function (files, index) {
    if (files != null) {
        var file = files[0];
        var index = this.$index;
        if ($scope.fileReaderSupported && file.type.indexOf('image') > -1) {
            $timeout(function () {
                var fileReader = new FileReader();
                fileReader.readAsDataURL(file);
                fileReader.onload = function (e) {
                    $timeout(function () {
                        $scope.thumbnail[index] = {dataUrl: e.target.result};
                    });
                }
            });
        }
    }
};

Demo: http://jsfiddle.net/m2qagzzk/3/

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah u are right. The image source they get is same so the image repeat in the all row. Thank u very much
@dfsq I think your answer is the best from other about uploading multiple files in angular, but I have one question about your fiddle, is possible to display file path in input ng-model? Maybe using track by $index?
No file path but file name
@Mat.Now It's simpler to put names into separate model object without ngModel here.
@dfsq ok, but now I have one single input file, and I use ng-model to save file name in database but I am not sure that is the best solution to use ngModel to display file name in input and next save in database

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.