How to set data in localStorage

Asked

Viewed 3,297 times

1

I have the following code:

$scope.loginUser = function (user) {
    console.log(user);
    $http.post("admin/php/login.php", user).then(function(response){

    if(typeof(Storage) !== "undefined") {
            localStorage.setItem("chave", "valor - algum conteudo aqui");
            var valor = localStorage.getItem("chave");
            console.log(valor);
        } else {
            console.log("Desculpe, mas o navegador nao possui suporte a Web Storage.");
        }
        $window.localStorage.setItem("idUsuarios",idUsuarios);
        $window.localStorage.setItem("nome",nome);
        $window.localStorage.setItem("email",email);

        //console.log(response);
        $location.path('admin/views/painel');

    })
}

How should I set the data for those stored in localStorage?

$window.localStorage.setItem("idUsuarios",?);

PHP:

<?php
$data = json_decode(file_get_contents("php://input"));
$email = $data->email;
$senha = $data->senha;

$sql = mysqli_query($con, "SELECT * FROM usuarios WHERE email='$email' AND senha='$senha' ");

$return = array();

while($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){
      $return = $row;
}

echo json_encode($return);
  • Now that I went to see, the angular provides a localStorage ? what is its difference to the javascript default ?

2 answers

1

localStorage records only texts, so it is necessary to have the information in text format that can be obtained with the JSON.stringify if necessary.

var test = ["Hello", "World", "Item 3", 5];

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

    //Gravando no localStorage
    localStorage.setItem("Teste", JSON.stringify(test));
    //

    //lendo do localStorage
    var itens = JSON.parse(localStorage.getItem("Teste"));
    //

    for (var s of itens) {
      var node = document.createElement("LI");
      node.innerHTML = s;
      document.getElementById("result").appendChild(node);
    };
} else {
    var node = document.createElement("LI");
    node.innerHTML = "'localStorage' Não suportado";
    document.getElementById("result").appendChild(node);
}

Here is an example from Jsfiddle

  • localStorage is key/value, in value and accepted only texts, if there is a need to insert an array it will be necessary to use JSON.stringify, and interesting to put this information as additional so that it does not end up using it in texts that are not needed

  • But where do I use JSON.stringify? In this case, here, you put the data in hand "var test = ["Hello", "World", "Item 3", 5];", my data is coming from php, from $http.post ("admin/php/login.php", user). then(Function(Sponse), understand?

  • Got it Leandro, I’m doing this: $window.localStorage.setItem("idUsuarios", JSON.stringify({idUsuarios: Response}); but instead of just saving the id, you’re saving everything you have in Sponse

  • have tried $window.localStorage.setItem("idUsuarios", JSON.stringify({idUsuarios: response.id})); ?

  • I found, use, at angular: $window.localStorage.setItem("idUsuarios", Sponse.data.idUsuarios);

0

First thing add a condition to check if the browser has support, then use setItem() to insert an item in localStorage, this function accepts two parameters, which are key and value.

Example:

var id = 1;
var nome = "Gabriel";
var email = "[email protected]";
if (typeof(Storage) !== "undefined") {
  localStorage.setItem("id", id);
  localStorage.setItem("nome", nome);
  localStorage.setItem("email", email);
  print("id");
  print("nome");
  print("email");
} else {
  console.log("Desculpe, mas o navegador nao possui suporte a Web Storage.");
}

function print(key) {
  document.getElementById("result").innerHTML += localStorage.getItem(key) + "<br>";
}
<div id="result"></div>

See working on jsfiddle

  • I already put the check... But what value, content I put in "value - some content here"?

  • How do I indicate that I want to put the id, in the first, name, in the second and email in the third?

  • where this "value - some content here" Voce puts its value/ variable etc... in the key the identification why when Voce does a getItem("key") Voce will receive the value it placed, paste it into jsfiddle and see the console

  • But I am, first, taking the data from the php backend that is being returned, to put, for the first time, in localStorage, understand? And don’t take the data that’s already local.

  • Do you use ajax to get the backend data? the backend and in php ?

  • In the code you posted, you put "in hand" the data in the variable... But I want the system seven the values of the variable, check?

  • and how Voce takes the system values ?

  • No, I am not using ajax. And yes, I am using php... I will post my code in php.

Show 3 more comments

Browser other questions tagged

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