我有JSON
数组看起来像这样
{
"-K3OFVYXVUiT-oYXFmgX": {
"id": 4658,
"username": "shamon"
},
"-K3OFZAt9Qyy9v6z-XYQ": {
"id": 9891,
"username": "manu"
},
"-K3OFafyCi6g9UhdR2mA": {
"id": 7219,
"username": "hari"
},
"-K3OGYGGry3pU8_Qkjg_": {
"id": 8028,
"username": "shamonsha"
}
}
我想在 <ul>
中显示用户名列表
<ul>
<li ng-repeat="user in userlist">{{user.username}}</li>
<ul>
但我会得到空结果
更新
这是我的完整代码,它是 firebase url 的响应形式,问题是我会得到 console.log($scope.userlist)
但它不会在 html 列表中更新
.controller('chatCtrl',function($scope){
messagesRef= new Firebase(chaturl+'userlist');
messagesRef.on('value', function (snapshot) {
var msg= snapshot.val();
$scope.userlist=JSON.stringify(msg);
console.log($scope.userlist);
});
});
最佳答案
您可以在表达式中使用(key, value)
<ul>
<li ng-repeat="(key, value) in userlist">
{{value.username}}
</li>
<ul>
编辑:需要使用$scope.$apply(fn)
以便更新更改。
.controller('chatCtrl',function($scope){
messagesRef= new Firebase(chaturl+'userlist');
messagesRef.on('value', function (snapshot) {
var msg= snapshot.val();
$scope.$apply(function () {
$scope.userlist=JSON.stringify(msg);
});
});
});
关于javascript - 从 Angular js 中的复杂 JSON 数组获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33773418/