0

I have the following string;

             $scope.Array="5678,9876,0988"

I am displaying it in my html as follows:

             <li ng-repeat="ArrayItem in Array.split(',')">
                    <input type="checkbox" >
                     {{Zip}}
             </li>

this displays all the string items separately along with a check box. I would like to know how to select multiple items from the list on the UI, and on the press of delete, delete these items from Array.

e.g. check 5678, 9876, and click Delete. The Array would now only have 0988.

1 Answer 1

2

angular.module('app', []).controller('ctrl', ['$scope', function($scope) {
    $scope.toDelete = {};
    $scope.Array="0988,9876,0988";
    
    $scope.delete = function(){      
      $scope.temp = $scope.Array.split(',');
      for(var prop in $scope.toDelete){        
        if($scope.toDelete[prop])
          $scope.temp[prop] = undefined;
      }      
      $scope.toDelete = {};
      $scope.Array = $scope.temp.filter(function(x){ return x !== undefined; }).join(',');
    }
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app='app' ng-controller="ctrl">
    <ul>
      <li ng-if='Array' ng-repeat="ArrayItem in Array.split(',') track by $index">
        <input type="checkbox" ng-model='toDelete[$index]'>{{ArrayItem}}
      </li>
    </ul>
    Array: {{Array | json}}
    <br>
    <input type='button' value='Delete' ng-click='delete()'/>
</div>

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

1 Comment

That was what I was looking for!

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.