Rahul Khatri
Rahul Khatri

Reputation: 287

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.

Upvotes: 1

Views: 2590

Answers (1)

Haymaker87
Haymaker87

Reputation: 529

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));
}

Upvotes: 3

Related Questions