0

Using an ajax request I receive the following json data:

{ 
    "status" : "success", 
    "xplist" : [
        { "rank" : "1", "username" : "test1", "xp" : "2500" },
        { "rank" : "2", "username" : "test2", "xp" : "2200" },
        { "rank" : "3", "username" : "test3", "xp" : "1900" }
     ]
 }

In my controller I created empty data variable

 $scope.data = {}; 

And assign the json data to it like this:

$scope.data.xpresult = data;

Then in my view I try to use ng-repeat to get the items out:

<li ng-repeat="xp in data.xpresult.xplist">                                             
     <div class="listItem">
         <div class="left">                                                          
             <p>{{xp.rank}}</p>
         </div>              
         <div class="middle">
             <p style="padding: 8px 46px 0px 0px;margin:0">{{xp.username}}</p>
             <p style="margin:0px">{{xp.xp}}</p>
          </div>          
    </div>
</li>     

I also tried the to repeat the following instead:

data.xpresult.0.xplist

Also tried to put the data in a variable and stringify the result again:

var xpdata = JSON.parse(data);
$scope.data.xpresult = JSON.stringify(xpdata.xplist);

But unfortunately this didn't work either.

Can anyone give me a pointer on how to get this out properly or what I am doing wrong?

Update

The controller is as following:

angular.module('AndriodApp')

.controller('ExperienceController', function($scope,md5){

$scope.data = {};

$scope.getTop50 = function(uid,uname) {
    var token = md5.createHash("test"+uid+uname+"test");
    var postdata = { messagetype: "getexperience", userid: uid, securitytoken: token };
    $.post("http://127.0.0.1/backend/jiomsg.php",postdata)
        .done(function( data ) {
            alert( "Data Loaded: " + data );
            $scope.data.xpresult = data;
    });
}

$scope.getTop50("1","Gerard");
});
3
  • 1
    may be data.xpresult.data.xplist. if "data" is from promise. Commented Jun 27, 2015 at 15:42
  • Where is your $http request defined ? Can you show us the call with $scope.data.xpresult = data; Are the HTML template & $scope assignment into same Ctrl? your html seems good (big code) Commented Jun 27, 2015 at 15:45
  • Updated my post with the controller. Commented Jun 27, 2015 at 15:54

1 Answer 1

2

You're sending a request and receiving a response behind the back of AngularJS, which has no idea you have modified the scope, and that the expressions in the page should thus be reevaluated.

Use the $http service, and everything will go fine:

$http.post("http://127.0.0.1/backend/jiomsg.php", postdata)
    .success(function(data) {
        alert( "Data Loaded: " + data );
        $scope.data.xpresult = data;
    });

Or, if using jquery is really necessary for whatever reason, make sure angular knows about the received response:

$.post("http://127.0.0.1/backend/jiomsg.php",postdata)
    .done(function( data ) {
        alert( "Data Loaded: " + data );
        $scope.data.xpresult = data;
        $scope.$apply();
});
Sign up to request clarification or add additional context in comments.

3 Comments

I used jquery because the $http.post didn't return any data. I tried your snippet there as well but it returns empty. I have added the $scope.$apply(); but it still won't display. Am I missing something else?
Use the debugger and inspect the value of data. Is it anobject or is it a string? If it is a string, you'll have to parse it with angular.fromJson().
Thanks a lot, that did the trick. It was indeed a string that needed to convert to json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.