V SH V SH - 2 months ago 20
reST (reStructuredText) Question

How to get array value of key from rest call response in angular js?

I am making a rest call in angular js. I am able to get simple key value from response. But when key's value in array form then I am not able to get value in angular js controller.

I am sharing my related code(Not Same).

Rc.all('demo/example/db/').get('quues/').then(function(res)
{
$scope.demo={}
$scope.demo.details={
value1 : res.Ksus.Type.Master.Origin.ID,
}

$scope.array={}
$scope.array.arrValue={
arrVal : res.Asus[0].AsuID,
}
}


JSON:

{
_id:123
Ksus: {
Type: {
Master: {
Origin: {
ID: "Demo"
}
}
}
},
Asus: [{
AsuID: "f4",
Type: "SU",
}]
}


I want to get AsuID value in controller. I am able to get simple key value without array in controller like ID: "Demo". But I want to get array value. When I tried arrVal : res.Asus[0], Then It is give 0 undefined value in console. Please give some idea for this thanks in advance.

Answer Source

This is because you are referencing res outside the get method. Below code will give you the required data.

 Rc.all('demo/example/db/').get('quues/').then(function(res)
  {
   $scope.demo={}
       $scope.demo.details={         
          value1 : res.Ksus.Type.Master.Origin.ID,        
       }
       $scope.array={}
       $scope.array.arrValue={         
          arrVal : res.Asus[0].AsuID,        
       }
    }