Cannot read property 'splice' of undefined


I'm trying to remove the item from the localstorage json data. but have error in my console 'splice' undefined. Please Help Me..

This is my Controller

app.controller('favgame', function($scope, $localStorage){
    $scope.saved = localStorage.getItem('favgame');
    var favgames = JSON.parse($scope.saved);
    $scope.onItemDelete = function(index) {

This is my ng-click code

<ion-delete-button class="button button-clear ion-minus-circled" ng-click="onItemDelete($index)">

Thank You!

Problem courtesy of: Praveen JP


Incorrect splice, instead use below. $scope.favgames, doesn't contain any values and is undefined.


Or set,

$scope.favgames = favgames.splice(index,1);
Solution courtesy of: Mahesh Sapkal


you have to change this line:

var favgames = JSON.parse($scope.saved);

with this one:

$scope.favgames = JSON.parse($scope.saved);
Discussion courtesy of: Fermín

Please change var favgames to var $scope.favgames

Discussion courtesy of: Ramesh Rajendran

This recipe can be found in it's original form on Stack Over Flow.