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

Asked

Viewed 1,078 times

3

I’m developing a system using angular js and php. The user inserts his Cpf and his password in a form, remembering that this user is registered in the database with his information, such as, address, name, etc. When user type his credentials, I take this data by js angular and forward 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 the 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 json to me, but how can I use this json on another Angular JS page ?

  • You are using ui-router or ngRouter?

  • I’m using the ngRouter.

  • See if you can get help http://stackoverflow.com/questions/25486392/best-way-to-pass-data-between-ngroute-controllers-from-one-view-to-the-next

  • later put a fiddle with example ...

3 answers

1

If you want to save this user, and the same doesn’t 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 this user a GET, 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....

1

One of the ways to do this is by using services.

In your main.js (where routes, controllers and 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 it into all the controllers where you will use the service through its identifier sharedThing, as 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.


1

Assuming your user object is something like this:

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

I would change the section 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!");
 });

Browser other questions tagged

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