0

I have a JSON file which contains data. I can print the data using ANgularJS. It will show in row with a checkbox and there is a Delete button. I want to delete the data from display and as well as from JSON file. Delete process would be like, Click on the checkbox which you want to Delete > Click on the Delete button. This is my plnkr link :-

http://plnkr.co/edit/A07XJk1RQNmhSnLFqLxH?p=preview

api.json is the JSON file.

This is the JSON file look like :-

{
  "1": {
    "venture": "XYZ Informatics",
    "member": [
      {
        "name": "abcd",
        "email": "[email protected]"
      }
    ],
    "message": "This is good day",
    "isclicked": false
  },
  "2": {
    "venture": "BBC Informatics",
    "member": [
      {
        "name": "xyz",
        "email": "[email protected]"
      }
    ],
    "message": "This is bad day",
    "isclicked": true
  }
}

2 Answers 2

1

Add ng-model to checkbox....then iterate data and use delete if it is checked

  $scope.delete = function() {
     angular.forEach($scope.datas, function(val, key) {
       if (val.isclicked) {
         delete $scope.datas[key];
       }
     })
   }

View

<form ng-repeat="data in datas">            
    <input type="checkbox" ng-model="data.isclicked">{{ data.venture }}          
</form>
<button ng-click="delete()">Delete</button>

DEMO

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

4 Comments

It is deleting item but not updating the JSON
What do you mean not updating the json? JSON is string data you get from server. $scope.datas has been updated... that's why view changes
To remove from file you need to send data to server and use server code to change file
Suppose, there is a API call /api/delete:id (Where id =1, 2), to delete. Then ?
1

Assuming you have your data is on the scope, you'd use the JavaScript delete function to remove an item from the array.

So...

delete $scope.data["1"]

Angular's digest should update anything watching that scope property automatically.

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.