Ahmed
Ahmed

Reputation: 255

How to delete element in object by index in Angular JS?

I have object: $scope.formData = {}

How I can delete element in object by $index:

 $scope.formData.university[$index];

I tried:

$scope.formData.university.splice($index);

Upvotes: 2

Views: 2602

Answers (2)

SVK
SVK

Reputation: 2197

You can use 'delete' operator. For more info Refer Link

 delete  $scope.formData.university[$index];

Upvotes: 1

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

It should have slice first parameter as $index & then second one would be 1

$scope.formData.university.splice($index, 1);

Note: using $index for deleting element would be risky if you are applying filtering on ng-repeat

Instead of that you could add some unique prop in your each element of ng-repeat you could add id in it, so that while deleting you could pass that id and then do find index of that element and delete that element from an array like the same way I did wrote above code.

Upvotes: 1

Related Questions