Problem in returning the answer via $ http.post ()

-2

I'm trying to get only a value of HTTP return but I'm not getting it.

.controller('myCtrl', function ($scope, $http) {
 $scope.sendPost = function() {
    var dataObj = {
            name : $scope.newName
    };  
    var res = $http.post('xxxx.consulta.php', dataObj)

    res.success(function(data, status, headers, config) {
        $scope.hello = data;
    });  
 }
})

When I run the page in the browser it returns me the object JSON :

[{"id":"13","nome":"Yuri","departamento":"EAD","img":"https:\/\/image.freepik.com\/icones-gratis\/perfil-macho-utilizador-sombra_318-40244.png"}]

I would like to save only the id and name, how do I? Already tried data.id or view {{hello.id}} and did not work ...

    
asked by anonymous 16.02.2016 / 02:25

2 answers

3

Your problem, as stated in the comments, occurs because you are returning an array, but you want to use only one object (as I understand it). For this you need to make a filter, or loop. See these examples:

angular.forEach(data, function(obj){
    if(sua logica aqui) {
        return obj; //retorne ou aplique o valor ao seu objeto.
    }
})

So you're interacting all over the array, going through each item and doing a check.

Or you can make a filter, see:

$scope.valor = $filter('filter')(data,{id: $scope.seuvalor});

The two methods will filter your array and assign only one value to the array. To use these methods, let's assume you want to catch the object that has the same name as your $scope.newName , so you would do this:

angular.forEach(data, function(obj){
    if(obj.name == $scope.newName) {
        return obj; //retorne ou aplique o valor ao seu objeto.
        //ou
        $scope.novoValor = obj; //Atribui todo o objeto ao $scope 
    }
})

//Ou então

$scope.novoValor = $filter('filter')(data,{name: $scope.newName});
    
16.02.2016 / 03:06
2

The concept is very simple, this is a JSON object, so you have to play this in a variable and use it as in the example below, you can either do query by position, as in a loop, by your controller, you can play on $scope.collection = data :

"collection" would be the same as a variable of type date, as below:

var data = [
  {"id":"13","nome":"Yuri","departamento":"EAD","img":"https:\/\/image.freepik.com\/icones-gratis\/perfil-macho-utilizador-sombra_318-40244.png"},

  {"id":"14","nome":"Yuri","departamento":"EAD","img":"https:\/\/image.freepik.com\/icones-gratis\/perfil-macho-utilizador-sombra_318-40244.png"}
]

//exemplo 1
console.log(data[0].id)
console.log(data[0].nome)

//exemplo 2
console.log(data[1].id)
console.log(data[1].nome)

//no laço:
for (var i in data) {
  console.log(data[i].nome)
  console.log(data[i].id)
}

In the view, you can use ng-repeat:

<div ng-repeat="dado in collection">
   {{ dado.id }} <br>
   {{ dado.nome }}
</div>

Or grab the position in the view:

<div>
       {{ collection[0].id }} <br>
       {{ collection[0].nome }}<hr>

       {{ collection[1].id }} <br>
       {{ collection[1].nome }}
</div>
    
16.02.2016 / 04:25