1

In multiple image upload how to delete particular image if i click that image in angularjs.i have uploaded multiple image and i have textbox with every uploaded image.if i click image delete icon it removes the image with that textbox.my view code looks below

  <input type="file" multiple file-upload /> {{files.length}}
  <div ng-repeat="step in files">
    <img ng-src="{{step.src}}" />
    <input type="text" ng-model="comment[$index]"><br>
    <input type="button" onclick="removeImage($index)" value="delete">
  </div>

In my angular controller code looks like below

app.directive('fileUpload', function() {
  return {
    scope: true, //create a new scope
    link: function(scope, el, attrs) {
      el.bind('change', function(event) {
        var files = event.target.files;
        //iterate files since 'multiple' may be specified on the element
        for (var i = 0; i < files.length; i++) {
          //emit event upward
          scope.$emit("fileSelected", {
            file: files[i]
          });
        }
      });
    }
  };
});
  $scope.files = [];
  $scope.$on("fileSelected", function(event, args) {
    var item = args;
    $scope.files.push(item);
    var reader = new FileReader();

    reader.addEventListener("load", function() {
      $scope.$apply(function() {
        item.src = reader.result;
      });
    }, false);

    if (item.file) {
      reader.readAsDataURL(item.file);
    }
  });

1 Answer 1

1

Instead of onclick use ng-click and use javascript splice function to remove an item from an array

<input type="button" ng-click="removeImage($index)" value="delete">

$scope.removeImage = function(index){
   $scope.files.splice(index,1)
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much..if u donot mind could you consider below question stackoverflow.com/questions/46250848/…

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.