AngularJS 获取对象和数字
AngularJS GET Object and Number
我正在尝试 return 来自 json API 的对象,但我遇到了问题,我需要 return “24h_volume_usd” 来自https://api.coinmarketcap.com/v1/ticker/?limit=0
但我用了这个表达式 =
$http.get("https://api.coinmarketcap.com/v1/ticker/?limit=0").then(function(resp){
if(resp){
$scope.moedas = resp.data;
};
});
当我尝试使用 ng-repeat { moeda in moedas } 时,我使用
得到 null
{{ moeda.24h_volume_usd }}
你能帮帮我吗!
在 JavaScript 中,如果对象 属性 键以数字开头,则不能使用点语法通过标识符引用对象 属性。为了访问此 属性,您必须将密钥转换为字符串,然后使用括号语法引用 属性。
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).
所以,而不是使用:
moeda.24h_volume_usd
您将使用:
moeda['24h_volume_usd']
我正在尝试 return 来自 json API 的对象,但我遇到了问题,我需要 return “24h_volume_usd” 来自https://api.coinmarketcap.com/v1/ticker/?limit=0
但我用了这个表达式 =
$http.get("https://api.coinmarketcap.com/v1/ticker/?limit=0").then(function(resp){
if(resp){
$scope.moedas = resp.data;
};
});
当我尝试使用 ng-repeat { moeda in moedas } 时,我使用
得到 null{{ moeda.24h_volume_usd }}
你能帮帮我吗!
在 JavaScript 中,如果对象 属性 键以数字开头,则不能使用点语法通过标识符引用对象 属性。为了访问此 属性,您必须将密钥转换为字符串,然后使用括号语法引用 属性。
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).
所以,而不是使用:
moeda.24h_volume_usd
您将使用:
moeda['24h_volume_usd']