Why can’t I use $window.localStorage.setItem() with angular?

Asked

Viewed 1,014 times

1

After registering, I want to temporarily put some data in localStorage, but I’m not getting.

Follows my code:

app.controller('cadastroCtrl', ['$scope', '$stateParams', '$http', '$cordovaSQLite', '$window', '$state', function ($scope, $stateParams, $http, $cordovaSQLite, $window, $state) {

$scope.email = [];

$scope.cadastrar = function(usuario){

$http.post("http://vigilantescomunitarios.com/serviapp/api_gustavo/register.php", usuario).success(function(response){

    var nome = response.nome;
    var email = response.email;
    var id = response.idusuario;
    var is_professional = response.prof;

    if(typeof(Storage) !== "undefined"){    

        if(is_professional == 1){
            $window.localStorage.setItem('userPro', nome);
            $scope.email = $window.localStorage.setItem('emailPro', email);
            $window.localStorage.setItem('idPro', id);

            console.log('Profissional');

            $state.go('menuProfissional');
        }else{
            $window.localStorage.setItem('userCli', nome);
            $scope.email = $window.localStorage.setItem('emailCli', email);
            $window.localStorage.setItem('idCli', id);

            console.log('Cliente');

            $state.go('menuCliente');
        }
    }else{
        console.log("Desculpe, mas o navegador nao possui suporte a Web Storage.");
    }

    })
}

}])

inserir a descrição da imagem aqui Neither does the local url appear "localhost:8080..."

  • Not getting why? Makes some mistake?

  • Pq I check in the "Application" tab of the Chrome console and nothing appears in "Local Storage", where it usually appears.

  • Do you have any idea @jbueno?

  • Make sure you are not associating any Undefined value in localStorage, in which case it is not saved at all. See if the variables name, email and id are not Undefined

  • No, @Rodolfojorgenemernogueira, even that does not appear.

  • Are you sure you’re getting into ifs?

Show 1 more comment

1 answer

1

Try before saving the item in localStorage, turn it into String

 $window.localStorage.setItem('emailPro', JSON.stringify(email))

Then to recover you use this function to transform into a new object:

JSON.parse($window.localStorage.getItem('emailPro'))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.