Ionic Angular Js Get Wordpress comments nested way


#1

I am working on Ionic app for my wordpress site. Currently I am able to get the posts content, now what I need is to show the comments for each post. This link will show all the comments for one of the posts. http://cookingshooking.com/wp-json/posts/2054/comments

You can see at the end of this question how the json data is returned

In my controller i am getting the comments based on the post id and currently I am using the below html to show the comments, which just iterates to one child level. because parent id of second level of comment that is reply to an comment will not be 0

<ion-list>
<ion-item class="comments" ng-repeat="comment in comments | orderBy:'date'" ng-if="comment.parent==0">
<div ng-bind-html="comment.content"></div>

 <ion-list>
<ion-item class="comments" ng-repeat="commentchild in comments | orderBy:'date'" ng-if="commentchild.parent==comment.ID">
<div ng-bind-html="commentchild.content"></div>

</ion-list>
</ion-list>

how can i show all the comments in a properly nested way like this

Json Data what I get from server

[
{
    "ID": 3344,
    "post": 2054,
    "content": "<p>Reply 2 to Comment 3</p>\n",
    "status": "approved",
    "type": "comment",
    "parent": 3295,
    "author": {
        "ID": 1,
        "username": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "admin",
        "slug": "admin",
        "URL": "",
        "avatar": "http://2.gravatar.com/avatar/544b03528ffbb99ac97ec7f5499c1ca9?s=96",
        "description": "",
        "registered": "2015-04-09T18:43:36+00:00",
        "meta": {
            "links": {
                "self": "http://cookingshooking.com/wp-json/users/1",
                "archives": "http://cookingshooking.com/wp-json/users/1/posts"
            }
        }
    },
    "date": "2015-07-05T13:55:26",
    "date_tz": "UTC",
    "date_gmt": "2015-07-05T13:55:26",
    "meta": {
        "links": {
            "up": "http://cookingshooking.com/wp-json/posts/2054",
            "in-reply-to": "http://cookingshooking.com/wp-json/posts/2054/comments/3295",
            "self": "http://cookingshooking.com/wp-json/posts/2054/comments/3344"
        }
    }
},
{
    "ID": 3343,
    "post": 2054,
    "content": "<p>Reply 1 to  Reply 1 of Comment 3</p>\n",
    "status": "approved",
    "type": "comment",
    "parent": 3298,
    "author": {
        "ID": 1,
        "username": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "admin",
        "slug": "admin",
        "URL": "",
        "avatar": "http://2.gravatar.com/avatar/544b03528ffbb99ac97ec7f5499c1ca9?s=96",
        "description": "",
        "registered": "2015-04-09T18:43:36+00:00",
        "meta": {
            "links": {
                "self": "http://cookingshooking.com/wp-json/users/1",
                "archives": "http://cookingshooking.com/wp-json/users/1/posts"
            }
        }
    },
    "date": "2015-07-05T13:54:39",
    "date_tz": "UTC",
    "date_gmt": "2015-07-05T13:54:39",
    "meta": {
        "links": {
            "up": "http://cookingshooking.com/wp-json/posts/2054",
            "in-reply-to": "http://cookingshooking.com/wp-json/posts/2054/comments/3298",
            "self": "http://cookingshooking.com/wp-json/posts/2054/comments/3343"
        }
    }
},
{
    "ID": 3342,
    "post": 2054,
    "content": "<p>Reply 1 to Reply 1 of Comment 2</p>\n",
    "status": "approved",
    "type": "comment",
    "parent": 2594,
    "author": {
        "ID": 1,
        "username": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "admin",
        "slug": "admin",
        "URL": "",
        "avatar": "http://2.gravatar.com/avatar/544b03528ffbb99ac97ec7f5499c1ca9?s=96",
        "description": "",
        "registered": "2015-04-09T18:43:36+00:00",
        "meta": {
            "links": {
                "self": "http://cookingshooking.com/wp-json/users/1",
                "archives": "http://cookingshooking.com/wp-json/users/1/posts"
            }
        }
    },
    "date": "2015-07-05T13:53:42",
    "date_tz": "UTC",
    "date_gmt": "2015-07-05T13:53:42",
    "meta": {
        "links": {
            "up": "http://cookingshooking.com/wp-json/posts/2054",
            "in-reply-to": "http://cookingshooking.com/wp-json/posts/2054/comments/2594",
            "self": "http://cookingshooking.com/wp-json/posts/2054/comments/3342"
        }
    }
},
{
    "ID": 2594,
    "post": 2054,
    "content": "<p>Reply 1 to comment 2</p>\n",
    "status": "approved",
    "type": "comment",
    "parent": 2591,
    "author": {
        "ID": 2,
        "username": "Yaman",
        "name": "Yaman",
        "first_name": "Yaman",
        "last_name": "",
        "nickname": "Yaman",
        "slug": "yaman",
        "URL": "",
        "avatar": "http://2.gravatar.com/avatar/819fe4e0218f0a173f74502b86ef563c?s=96",
        "description": "",
        "registered": "2015-04-13T09:37:02+00:00",
        "meta": {
            "links": {
                "self": "http://cookingshooking.com/wp-json/users/2",
                "archives": "http://cookingshooking.com/wp-json/users/2/posts"
            }
        }
    },
    "date": "2014-11-26T13:44:39",
    "date_tz": "UTC",
    "date_gmt": "2014-11-26T13:44:39",
    "meta": {
        "links": {
            "up": "http://cookingshooking.com/wp-json/posts/2054",
            "in-reply-to": "http://cookingshooking.com/wp-json/posts/2054/comments/2591",
            "self": "http://cookingshooking.com/wp-json/posts/2054/comments/2594"
        }
    }
},
{
    "ID": 1879,
    "post": 2054,
    "content": "<p>Reply 1 to comment 1</p>\n",
    "status": "approved",
    "type": "comment",
    "parent": 1876,
    "author": {
        "ID": 2,
        "username": "Yaman",
        "name": "Yaman",
        "first_name": "Yaman",
        "last_name": "",
        "nickname": "Yaman",
        "slug": "yaman",
        "URL": "",
        "avatar": "http://2.gravatar.com/avatar/819fe4e0218f0a173f74502b86ef563c?s=96",
        "description": "",
        "registered": "2015-04-13T09:37:02+00:00",
        "meta": {
            "links": {
                "self": "http://cookingshooking.com/wp-json/users/2",
                "archives": "http://cookingshooking.com/wp-json/users/2/posts"
            }
        }
    },
    "date": "2014-11-26T13:44:39",
    "date_tz": "UTC",
    "date_gmt": "2014-11-26T13:44:39",
    "meta": {
        "links": {
            "up": "http://cookingshooking.com/wp-json/posts/2054",
            "in-reply-to": "http://cookingshooking.com/wp-json/posts/2054/comments/1876",
            "self": "http://cookingshooking.com/wp-json/posts/2054/comments/1879"
        }
    }
},
{
    "ID": 3298,
    "post": 2054,
    "content": "<p>Reply 1 Comment 3</p>\n",
    "status": "approved",
    "type": "comment",
    "parent": 3295,
    "author": {
        "ID": 2,
        "username": "Yaman",
        "name": "Yaman",
        "first_name": "Yaman",
        "last_name": "",
        "nickname": "Yaman",
        "slug": "yaman",
        "URL": "",
        "avatar": "http://2.gravatar.com/avatar/819fe4e0218f0a173f74502b86ef563c?s=96",
        "description": "",
        "registered": "2015-04-13T09:37:02+00:00",
        "meta": {
            "links": {
                "self": "http://cookingshooking.com/wp-json/users/2",
                "archives": "http://cookingshooking.com/wp-json/users/2/posts"
            }
        }
    },
    "date": "2014-11-26T13:44:39",
    "date_tz": "UTC",
    "date_gmt": "2014-11-26T13:44:39",
    "meta": {
        "links": {
            "up": "http://cookingshooking.com/wp-json/posts/2054",
            "in-reply-to": "http://cookingshooking.com/wp-json/posts/2054/comments/3295",
            "self": "http://cookingshooking.com/wp-json/posts/2054/comments/3298"
        }
    }
},
{
    "ID": 2591,
    "post": 2054,
    "content": "<p>Comment 2</p>\n",
    "status": "approved",
    "type": "comment",
    "parent": 0,
    "author": {
        "ID": 0,
        "name": "Aditya",
        "URL": "",
        "avatar": "http://2.gravatar.com/avatar/88dd02fbd516dcc7a7d16e85afa13114?s=96"
    },
    "date": "2014-11-06T10:27:58",
    "date_tz": "UTC",
    "date_gmt": "2014-11-06T10:27:58",
    "meta": {
        "links": {
            "up": "http://cookingshooking.com/wp-json/posts/2054",
            "self": "http://cookingshooking.com/wp-json/posts/2054/comments/2591"
        }
    }
},
{
    "ID": 1876,
    "post": 2054,
    "content": "<p>Comment 1</p>\n",
    "status": "approved",
    "type": "comment",
    "parent": 0,
    "author": {
        "ID": 0,
        "name": "Aditya",
        "URL": "",
        "avatar": "http://2.gravatar.com/avatar/88dd02fbd516dcc7a7d16e85afa13114?s=96"
    },
    "date": "2014-11-06T10:27:58",
    "date_tz": "UTC",
    "date_gmt": "2014-11-06T10:27:58",
    "meta": {
        "links": {
            "up": "http://cookingshooking.com/wp-json/posts/2054",
            "self": "http://cookingshooking.com/wp-json/posts/2054/comments/1876"
        }
    }
},
{
    "ID": 3295,
    "post": 2054,
    "content": "<p>Comment 3</p>\n",
    "status": "approved",
    "type": "comment",
    "parent": 0,
    "author": {
        "ID": 0,
        "name": "Aditya",
        "URL": "",
        "avatar": "http://2.gravatar.com/avatar/88dd02fbd516dcc7a7d16e85afa13114?s=96"
    },
    "date": "2014-11-06T10:27:58",
    "date_tz": "UTC",
    "date_gmt": "2014-11-06T10:27:58",
    "meta": {
        "links": {
            "up": "http://cookingshooking.com/wp-json/posts/2054",
            "self": "http://cookingshooking.com/wp-json/posts/2054/comments/3295"
        }
    }
}
]

#2

Not sure is the information not sufficient here? if anyone can look into it that will be great


#3

Hi Team, Can anyone suggest any alternate to this?

I have written code to get 5 child items by using the normal loop which I know is not a proper solution.
I go like this commentchild1, commentchild2 etc: