Rahul Khatri Rahul Khatri - 4 months ago 80
Javascript Question

How do I remove a specific element from local storage array in Angularjs?

Here's my HTML:

<tr ng-repeat="student in students track by $index">
//some code here
<button ng-click="remove(student)">Delete</button>
</td>
</tr>


And here's my .js file code for deleting a student(not from local storage):

$scope.remove = function(student) {
var index = $scope.students.indexOf(student);
$scope.students.splice(index, 1);
}


How do I access local storage from my js code and delete a particular student from local storage.

Answer

Sounds like you should rephrase your question to be "How do I access browser's local storage from js code?"

You can access localStorage in your js code with localStorage. I would delete your student from $scope.students and when finished, set the new array as an item in local storage:

$scope.remove = function(student) {
    var index = $scope.students.indexOf(student);
    $scope.students.splice(index, 1);
    localStorage.setItem('students', JSON.stringify($scope.students));
}