Base on your updated question the if
condition does not make sense, because you set $scope.fav
to []
right before, so it can never be "undefined"
.
Most likely you want to have your test that way:
if (typeof localStorage["fav"] !== "undefined") {
$scope.fav = JSON.parse(localStorage["fav"]);
}
As i don't know if there is a situation where localStorage["fav"]
could contain the string "undefined"
you probably also need test for this.
if (typeof localStorage["fav"] !== "undefined"
&& localStorage["fav"] !== "undefined") {
$scope.fav = JSON.parse(localStorage["fav"]);
}