-1

Can anyone help me in parsing below JSON Request using ng-repeat

I want to know how to use ng-repeat in HTML to get the description of the troubles in JSON

Using below code I am getting whole Object of trobles

posts.json

    {
  "persons": [
    {
      "properties": {
        "label": "checked",
        "type": "young"
      },
      "personType": "average",
      "troubles": [],
      "externalResourceLinks": []
    },
    {
      "properties": {
        "label": "checked",
        "type": "aged"
      },
      "personType": "bad",
      "troubles": [
        {
          "name": "Rocky",
          "description": "Health problem",
          "criticality": false,
          "date": "2016-08-07T08:43:28+0000",
          "longDate": 1470559408519
        }
      ]
    }
  ]
}

in HTML I am using

<tr>
<td class="features" ng-repeat="list in person">{{list.persontype}}</td>
</tr>
<tr>
<td class="features" ng-repeat="list in person">{{list.troubles}}</td>
</tr>

Angular Function

var app = angular.module('myApp', ["ngTable"]);
app.controller('PostsCtrlAjax', ['$scope', '$http', function($scope, $http) {
        $http({
            method: 'POST',
            url: 'scripts/posts.json'
        }).success(function(data) {
          $scope.post = data;
          persons = data['persons'];
                    $scope.person = persons;
        })
    }

]);

1 Answer 1

1

You have an array of troubles so as an example based on your JSON this would work:

Your main module:

// app.js
(function() {

    angular.module('myApp', ["ngTable"]);

})();

Your controller:

// PostsCtrlAjax.js
(function() {

    angular.module('myApp').controller('PostsCtrlAjax', PostsCtrlAjax);

    PostsCtrlAjax.$inject = ['$scope', '$http'];

    function PostsCtrlAjax($scope, $http) {

        getPersons();

        function getPersons() {

            $http({
                method: 'POST',
                url: 'scripts/posts.json'
            }).then(function(response) {

                $scope.post = response.data;

            }, function(errors) {

                // any error handling goes here

            });

        }

    }

})();

Your view:

<!-- your other html here -->

<tr>
    <td class="features" ng-repeat="person in post.persons">{{person.persontype}}</td>
</tr>
<tr>
    <td class="features" ng-repeat="person in post.persons">
        <p ng-repeat="trouble in person.troubles">{{trouble.description}}</p>
    </td>
</tr>

<!-- the rest of your html here -->

I hope you don't mind me tidying up your code a bit to be a bit more semantic.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you please help in repeating through troubles using ng-repeat
How did you get on?
Hi Thanks a lot for ur answer,, I am not able to get output using the above, Is there something missing
I have just updated my answer. Change $scope.post = response to $scope.post = response.data; in your controller.

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.