1

I have an object which has comments and have replies obj that replies obj further have nested replies obj. How I can render it on ng-repeat. I have tried this solution, it makes my replies object undefined. e.g this

<div  ng-repeat="it in item.CommentReplies=it.CommentReplies  track by $index" >

why it making my object undefined is it right solution ? here is my object enter image description here

6
  • is item an array? Commented Aug 1, 2017 at 14:40
  • ys array of objects @digit Commented Aug 1, 2017 at 14:40
  • please post your fakedata as example a.e. item Commented Aug 1, 2017 at 14:49
  • picture shown is item (obj of array) @MaximShoustin has nested obj Commented Aug 1, 2017 at 14:54
  • 1
    @Asad I think you have enough reputation to put object as code instead post image. It will help us a lot to track down the issue Commented Aug 1, 2017 at 14:57

1 Answer 1

1

You should use two ng-repeat like this :

<div class="replylist" ng-repeat="it in item.CommentReplies track by $index">
{{it.Text}}
      <div ng-repeat="comment in it.CommentReplies">
      {{comment.Text}}
      </div>
</div>

Hope this helps. Would have left a comment but don't have enough reputation.

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

5 Comments

what if there is nested json in it.CommentReplies ?
If I understood correctly what you say, you can have an unlimited number of nested array of CommentReplies in each CommentReplies item ?
If that's what you're trying to do, I don't think it's possible with ng-repeat, but you can write a recursive function which builds the html structure and then call it inside your html code, like this : $scope.displayComment = function (item) { $scope.htmlResult += item.Text + '<br/>'; for(var i = 0; i < item.CommentReplies.length: i++) { $scope.displayComment(item.CommentReplies[i]); } } <div class="replylist" ng-init="displayComment(item)"> {{htmlResult}} </div> I did not test my code, but if you can give us a sample of your data I could test it.
hi do you check the solution link in my question that i want to try @hmk
Based on your link apparently it's possible with ng-repeat, he uses 2 ng-repeat to create a recursive call.In your case you would put this somewhere to call the structure the first time : <div ng-repeat="item in items" ng-include="'comment.html'" > And you create a view comment.html,and put the second ng-repeat in it,with ng-include to let it call itself: <div ng-repeat="item in comments = item.CommentReplies" ng-include="'comment.html'" ng-init="level = level + 1"> "comments" is used just another name for item.CommentReplies,I don't think you need it,he uses it bcs he applies an orderby

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.