Elnaz
Elnaz

Reputation: 2890

Refreshing ng-repeat without $scope, without new request to sever

I have a table with this content:

 <tbody>
     <tr ng-repeat="result in ctrl.result track by $index">
       <td ng-bind="$index+1"></td>
       <td ng-bind="::result.title"></td>
       <td> <button type="button" class="btn" ng-click='ctrl.deleteItem(result)'>Delete</button></td>
     </tr>
 </tbody>  

And in my controller I have:

 vm.deleteItem = function (result) {
        myService.deleteItem(result.id)
           .then(function (response) {
                vm.result.splice(result, 1);
            });
    };

As you see, the vm.result has changed if the item be deleted successfully. Now, the item deleted in db, so we have response and then the item has been removed from vm.result, too. But the list hasn't updated in browser.
As you see, I use controller as approach and not $scope .

Upvotes: 1

Views: 289

Answers (2)

Gayathri Mohan
Gayathri Mohan

Reputation: 2962

     var index = vm.result.findIndex(function(item) {
                if (item.id == test.id) {
                    return true;
                }
            });

            if (index !== -1) {
                // Removing the test from list;
                vm.result.splice(index, 1);
}

check based on id from the array test.id is the id of the element u r deleting

Upvotes: 0

Alexander Kravets
Alexander Kravets

Reputation: 4385

Try deleting the item this way:

vm.result.splice(vm.result.indexOf(result), 1);

The first Splice parameter should be the index of element rather than element itself.

Upvotes: 1

Related Questions