Search code examples
angularjsangularjs-scope

AngularJS GET Object and Number


I'm trying to return a object from json API and I had a problem, I need to return "24h_volume_usd" from https://api.coinmarketcap.com/v1/ticker/?limit=0

but I used this expression =

$http.get("https://api.coinmarketcap.com/v1/ticker/?limit=0").then(function(resp){
                if(resp){
                $scope.moedas = resp.data;
                };  
                });

when I tried to use the ng-repeat { moeda in moedas } ,I get null using

{{ moeda.24h_volume_usd }}

can you help me please!


Solution

  • In JavaScript, you cannot reference an object property by identifier using the dot syntax if the object property key starts with a number. In order to access this property, you must cast the key to a string, and then refer to the property using the bracket syntax.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Variables

    A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

    So, instead of using:

     moeda.24h_volume_usd
    

    You would use:

     moeda['24h_volume_usd']
    

    Working example: http://plnkr.co/edit/Ed7EYojM03z5PJHa4w6N?p=preview