1

i'm trying to iterate comments inside a nested json object with angular ng-repeat directive, but in somehow it seems not accessing to it, what i am doing wrong?

Here the json

        posts: [
        {
            title: 'Free pizza at club meetings',
            upvotes: 15,
            comments: [
                {commento:'comment1'},
                {commento:'comment2'}
            ]
        },
        {
            title: 'End all club emails with Laffy Taffy jokes',
            upvotes: 9,
            comments: [],
        }
];

and here the view

    <div class="col-lg-12 col-xs-12 comment" ng-repeat="post in posts | orderBy:'-upvotes'">
    <p>{{post.title}}</p>
    <p>
        <span class="glyphicon glyphicon-plus-sign" ng-click="upVote(post)" style="cursor:pointer;"></span> Upvotes: {{post.upvotes}}
    </p>
    <p ng-repeat comment in post.comments>
    {{comment.commento}}
    </p>
</div>

the in the view give me error

3
  • 1
    ng-repeat="comment in post.comments" Commented Feb 15, 2017 at 9:34
  • Sorry @Azola I didn't see your comment before posting my answer. If you post an answer I'll delete mine so you get the rep. Commented Feb 15, 2017 at 9:43
  • Nothing to worry about @BernhardHofmann Commented Feb 15, 2017 at 9:44

4 Answers 4

2

Your syntax of ng-repeat is wrong:

<p ng-repeat comment in post.comments>

should be

<p ng-repeat="comment in post.comments">
Sign up to request clarification or add additional context in comments.

Comments

0

Just small syntax error- check the below code: JSFiddle

var app = angular.module("demoApp", []);

app.controller('myCtrl', function($scope){

    $scope.posts= [
            {
                title: 'Free pizza at club meetings',
                upvotes: 15,
                comments: [
                    {commento:'comment1'},
                    {commento:'comment2'}
                ]
            },
            {
                title: 'End all club emails with Laffy Taffy jokes',
                upvotes: 9,
                comments: [{commento:'comment3'},
                    {commento:'comment4'}]
            }
    ];
		$scope.upVote = function(post){}
});
<div ng-app="demoApp" ng-controller="myCtrl">
<div ng-repeat="post in posts">
    <span>{{post.title}}</span>
    <p class="glyphicon glyphicon-plus-sign" ng-click="upVote(post)" style="cursor:pointer;"></p> Upvotes: {{post.upvotes}}
    
    <p ng-repeat = "comment in post.comments">
        {{comment.commento}}
    </p>
</div>
</div>

Comments

0

ok was only an formatted error

<p ng-repeat="comment in post.comments">

1 Comment

Please mark the answer that resolved the issue for you (tick mark) so that the question doesn't remain "unanswered". Unanswered questions sometimes make people spend time looking at the (already answered) question when they could be helping someone else.
0

Observations :

  • Use <p ng-repeat="comment in post.comments"> instead of <p ng-repeat comment in post.comments>.
  • $scope.posts object is not declared correctly. use $scope.posts = [] instead of $scope.posts: [].

DEMO

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl',function($scope) {
    $scope.posts = [
        {
            title: 'Free pizza at club meetings',
            upvotes: 15,
            comments: [
                {commento:'comment1'},
                {commento:'comment2'}
            ]
        },
        {
            title: 'End all club emails with Laffy Taffy jokes',
            upvotes: 9,
            comments: [],
        }
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<div class="col-lg-12 col-xs-12 comment" ng-repeat="post in posts | orderBy:'-upvotes'">
    <p>{{post.title}}</p>
    <p>
        <span class="glyphicon glyphicon-plus-sign" ng-click="upVote(post)" style="cursor:pointer;"></span> Upvotes: {{post.upvotes}}
    </p>
    <p ng-repeat="comment in post.comments">
    {{comment.commento}}
    </p>
</div>
</div>

Comments

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.