Blessan Kurien
Blessan Kurien

Reputation: 1665

Getting value from complex JSON array in angular js

I have JSON array look like this

{
    "-K3OFVYXVUiT-oYXFmgX": {
        "id": 4658,
        "username": "shamon"
    },
    "-K3OFZAt9Qyy9v6z-XYQ": {
        "id": 9891,
        "username": "manu"
    },
    "-K3OFafyCi6g9UhdR2mA": {
        "id": 7219,
        "username": "hari"
    },
    "-K3OGYGGry3pU8_Qkjg_": {
        "id": 8028,
        "username": "shamonsha"
    }
}

I want to display the usernames in a <ul> list

<ul>
    <li ng-repeat="user in userlist">{{user.username}}</li>
<ul>

But I will get empty result

UPDATE

Here is my full code,its a response form firebase url,the problem is i will get console.log($scope.userlist) but it will not updated in html list

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

Upvotes: 0

Views: 70

Answers (1)

Satpal
Satpal

Reputation: 133453

You can use (key, value) in expression

<ul>
    <li ng-repeat="(key, value) in userlist">
        {{value.username}}
    </li>
<ul>

DEMO

EDIT: Need to use $scope.$apply(fn) so that changes is updated.

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

Upvotes: 1

Related Questions