AngularJS: error in Array, JSON Service

2

I want to take the first record out of the array but I get the following error:

This is my code:

servicioMiClaro.obtenerDirecciones().then(function (response) {
           $scope.listadoProductoServicios  = response.data;
            console.log(response.data);
        }, function (error) {
            $scope.status = 'Unable to load customer data: ' + error.message;
        });

console.log( $scope.listadoProductoServicios.listadoProductoServicios[0].afiliado);}
    
asked by jorgenc 15.06.2016 в 22:33
source

1 answer

1

You are accessing the first record in the array well.

$scope.listadoProductoServicios.listadoProductoServicios[0].afiliado

But the console.log() is placing it outside of where you assign the values. Angular is based on Javascript, therefore it is asynchronous, so it performs the search servicioMiClaro.obtenerDirecciones() while printing the console.log .

Try the following code:

servicioMiClaro.obtenerDirecciones().then(function (response) { 
  $scope.listadoProductoServicios  = response.data;
  console.log(response.data);
  console.log($scope.listadoProductoServicios.listadoProductoServicios[0].afiliado);
}, function (error) {
   $scope.status = 'Unable to load customer data: ' +error.message;
});
    
answered by 16.06.2016 в 18:50