how to parse angularjs data

1

It turns out that I make this request in the controllers to my php:

  $http({
  method: 'POST',
  url: 'http://localhost/nPanelarencion/app/php/consulta.php',
  headers: {
      'Content-Type': 'application/json', 
      'Accept': 'application/json' 
  },

})

which in console shows me its data in the following way

These same data I need to save them in $scope to be able to use them in html ...

Controller code:

 app.controller('search', function($timeout, $scope, $http) {

//     comienza la  conexion

getInfo();

function getInfo(){

  $http({
      method: 'POST',
      url: 'http://localhost/nPanelarencion/app/php/consulta.php',
      headers: {
          'Content-Type': 'application/json', /*or whatever type is relevant */
          'Accept': 'application/json' /* ditto */
      },
      data: {
       /* You probably need to send some data if you plan to log in */
   }
  })

.success(function(data){

    console.log(data);
    localStorage.setItem("Tickets", angular.fromJson(data));


//NEXT>

       $scope.loadUsers = function() {
         return $timeout(function() {
           $scope.tickets = localStorage.getItem('Tickets');

         }, 650);
       };
})};
conection.php:

<?php
// Connecting to database as mysqli_connect("hostname", "username", "password", "database name");
$con = mysqli_connect("localhost", "root", "", "saltala");
?>

consult.php:

<?php

// Including database connections
require_once 'conection.php';
// mysqli query to fetch all data from database
$query = "SELECT rut, numero, servicio_id FROM Tickets";
$result = mysqli_query($con, $query);
$arr = array();
if(mysqli_num_rows($result) != 0) {

while($row = mysqli_fetch_assoc($result)) {
$arr[] = $row;
 }
}
// Return json array containing data from the databasecon
echo $json_info = json_encode($arr);
?>

QUESTION: How do I do it? I tried to pause it and everything but it still does not work out.

I need to show the rout in a select

      <md-select placeholder="Assign to user" ng-model="buscar" md-on-open="loadUsers()" style="min-width: 200px;">
    <md-option ng-value="user" ng-repeat="tick in tickets | filter : buscar" >{{tick.rut}}</md-option>
  </md-select>
    
asked by Hernan Humaña 20.10.2016 в 21:32
source

2 answers

2

Your problem is not about angular. The functions localStorage.getItem and localStorage.setItem manipulate strings

  

A DOMString that contains the value of the key. If the key does not exist, it returns null.

What you should do is

$scope.loadUsers = function() {
     return $timeout(function() {

         $scope.tickets = JSON.parse(localStorage.getItem('Tickets'));
         ....

to convert the string back to object and

.success(function(data){

    localStorage.setItem("Tickets", JSON.stringify(data));
    ......

when saving these values.

Read How can I convert a JSON string to an object in JavaScript?

    
answered by 20.10.2016 / 22:27
source
1

Solve the previous problem, now that I have the data, I want to save it in LocalStorage but I overwrite the data, leaving only one ...

How do I save the data? in the controller, staying like this:

    $http({
    method: 'POST',
    url: 'http://localhost/nPanelarencion/app/php/consulta.php',
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
})

  .then(function(data) {
     var dat = data.data;

     for (var i = 0; i < dat.length; i++) {
       dat[i]
      var runum = dat[i].rut +","+ dat[i].numero
      console.log("show: ",runum);
      localStorage.setItem("rutnum",runum);
     }
  });

and console.log(runum) now show this:

Now, How do I receive the data? Well, in another controller, in the following function:

      function loadAll() {
    var rut = localStorage.getItem("rutnum").split(",")[0];
    console.log("el rut es: ",JSON.stringify(rut));


    var num = localStorage.getItem("rutnum").split(",")[1];
    console.log("El numero es: ",parseInt(num));
    var finalnum = parseInt(num)

    var repos = [
      {
        'rut'      : rut,
        'numero'   : finalnum

      }

    ];
    return repos.map( function (repo) {
      repo.value = repo.rut.toLowerCase();
      return repo;
    });
  }

And as I said before, the result is that it only shows me one single rut and a single number, I understand that it is about writing the previous one, how do I keep all the rout?

This is the image of the LocalStorage and what it shows me:

EDIT SIOESI

The error is in the following line:

 for (var i = 0; i < dat.length; i++) {
      var runum = dat[i].rut +","+ dat[i].numero
      console.log("show: ",runum);
      localStorage.setItem("rutnum",runum);
 }

You are going through your arrangement and keeping 1 for 1 THE SAME NAME OF THE KEY OF YOUR LOCALSTORAGE .. that's why the last one is always there You should do something like that

var tickets = [];
for (var i = 0; i < dat.length; i++) {
    var ticket = {id : dat[i].id, rut : dat[i].rut};
    tickets.push(ticket);
}
localStorage.setItem("Tickets",tickets);

So, you create a type of object in the array that has id and rut , you add it in the array tickets and then save in localStorage the array Tickets .. That array calls you call from your controller

$scope.tickets = localStorage.getItem('Tickets');

And now you can tour them

<div ng-repeat="ticket in tickets">
    {{ticket.rut}}
</div>
    
answered by 20.10.2016 в 22:48