What is the best way to pass JSON per parameter in Angular JS?

3

I'm developing a system using angular js and php. The user enters his cpf and his password in a form, remembering that this user is registered in the database with his information, like, address, name, etc. When user enter his credentials, I get this data by angular js and refer to php using $http.post('php/login.php',user); .

If the user's cpf is found in the database, I want it to return a JSON with all user data and pass by parameter using ngRoute with $location.path('pagina',+dados_usuarios) :

$http.get('php/login.php', user).success(function(data) {
  var result = JSON.stringify(data, null, 2);
  console.log(result);
  //$location.path('/dashboard/'+result);
}).error(function(data) {
  console.log("Usuário não cadastrado!");
});

PHP is returning me json, but how can I use this json on another page in Angular JS?

    
asked by anonymous 18.04.2016 / 21:24

3 answers

1

One of the ways to do this is using services .

In your main.js (where routes, controllers, etc. are declared) you can create a service with a getter and a setter .

app.service('sharedThing', function() {
  var stringValue = '';
  return {
    getString: function() {
      return stringValue;
    },
    setString: function(value) {
      stringValue = value;
    }
  }
});

Then you need to inject all controllers in which you will use service through its sharedThing identifier, just as the other dependencies are injected.

angular.module('myApp').controller('MyController', function ($scope, $http, $stateParams, sharedThing) { ... });

You can access the functions of service as follows.

sharedThing.setString('Olá Mundo!');

or use the stored as follows:

$scope.thing = sharedThing.getString();

Instead of passing a string like I did in the example, you can pass objects, arrays, etc.

18.04.2016 / 22:47
1

Assuming your user object looks something like this:

{
    "nome": "Roger Barreto",
    "idade": 34,
    "email": "[email protected]",
    "slug": "roger-barreto"
}

You would change the portion of your routine to:

$http.get('php/login.php', user).success(function(response) {
    $location.path('/dashboard/'+ response.data.slug);

 }).error(function(data) {
     console.log("Usuário não cadastrado!");
 });
    
19.04.2016 / 02:46
1

If you want to save this user, and it does not change, you can do this:

Controller

$http.post("sua url", data).success(function(response) {
     var user = response;
     localStorage.setItem("user", user); //primeiro parâmetro nome desse storage, e o segundo passe seu objeto
})

When you want to give a GET to this user, in your JS, you do:

var user = localStorage.getItem("user");

If you wanted to change this user, just remove this localStorage, and create another by passing the updated object, like this:

$http.put("sua url", data).success(function(response){
    var user = response;
    localStorage.removeItem("user"); //user é o local storage que já existe
    localStorage.setItem("user", user);
})

This is not the best solution, but it is one of them ... You can do, as pmargreff said, a service with gets and sets, or a model. You can also pass your user object through $ state.go ....

    
22.11.2016 / 13:56