Search code examples
phpangularjsjsonangularjs-ng-repeatangularjs-http

How to ng-repeat response data


I have a simple piece of code that sends data to php for processing and then it returns it. I would like to ng-repeat that data in the page, I just can't tell what isn't right. I must point out to the fact that I am but a novice, therefore I might be overlooking something..

So I have this in the controller:

  $scope.one = function () {
  $http({
  method: "POST",
  url: 'destination.php',
  data: {'message' : something},
  })
  .then(function(response)
  {
  $scope.message = response.data.results;
  }
  )}

This is how the data goes to PHP and back from it:

$params = json_decode(file_get_contents('php://input'), true);
$outp = "";
if ($outp != "") {$outp .= ",";}
$outp .= '{"REZCMD":"'  . $params["message"] . '"}';
$outp ='{ "results":[ '.$outp.' ] }';
echo $outp;

example:

{ "results":[ {"REZCMD":"somethingprocessed"} ] }

And this is the HTML

  <div ng-repeat="x in message track by $index"></div>
   <li>{{message}}
   <li>{{x.REZCMD}}

Which prints this: (example)

*[{"REZCMD":"1003372"}]
*

EDIT

For this result:

{ "results":[ {"REZCMD":"1003372"} ] }

What I am expecting should look like this:

  • 1003372


  • Solution

  • If you required to print only value then :

    <div>
       <li ng-repeat="obj in message">{{obj.REZCMD}}</li>
    </div>
    

    This method is listed in the docs: https://docs.angularjs.org/api/ng/directive/ngRepeat