簡體   English   中英

從angular js中的復雜JSON數組中獲取價值

[英]Getting value from complex JSON array in angular js

我的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>

但是我會得到空洞的結果

更新

這是我的完整代碼,其響應形式console.log($scope.userlist) 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) in expression使用(key, value) in expression

<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); 
              });
        });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM