Rafael Adel
Rafael Adel

Reputation: 7759

ngRepeat doesn't loop an array

I have this object:

{
    "_id" : ObjectId("5454e173cf8472d44e7df161"),
    "questionType" : 0,
    "question" : "question1",
    "answers" : [ 
        {
            "content" : "answer1"
        }, 
        {
            "is_true" : "true",
            "content" : "answer2"
        }, 
        {
            "content" : "answer3"
        }
    ],
    "matches" : [],
    "__v" : 0
}

And this controller:

var questionIndexController = function ($scope, Question, $routeParams) {
    var question = Question.get({id: $routeParams.id});
    $scope.question = question;
};


angular.module("adminMain")
    .controller("questionIndexController", ["$scope", "Question", "$routeParams", questionIndexController]);

And this markup:

<h3>Question:</h3>
<p>{{ question.question }}</p>
<ul>
    <li np-repeat="answer in question.answers">
        <span ng-show="answer.is_true">✓</span>
        <span>{{ answer.content }}</span>
    </li>
</ul>

But unfortunately the resulting html doesn't include any answers from the JSON. Meaning that the ngRepeat didn't loop through question.answers array.

<div ng-view="" class="ng-scope"><h3 class="ng-scope">Question:</h3>
<p class="ng-binding ng-scope">question1</p>
<ul class="ng-scope">
    <li np-repeat="answer in question.answers">
        <span ng-show="answer.is_true" class="ng-hide">✓</span>
        <span class="ng-binding"></span>
    </li>
</ul>
</div>

Any idea how to solve this ?


Edit

This is the Question factory code:

var questionFactory = function($resource, restUrls) {
    return $resource(restUrls.question.index, {}, {
        save: {
            url: restUrls.question.add,
            method: "POST",
            isArray: false
        }
    });
};

angular.module("adminMain").factory("Question", ["$resource", "restUrls", questionFactory]);

where restUrls.question.index is "/api/questions/:id"

And the node.js backend code for getting single question:

function indexQuestions(req, res, next) {
    var questionId = req.params.id || null;
    if(!questionId) next(errorHandler(404, "Not found"));
    Question.findOne({ _id: questionId }, function(err, question) {
        if(err) return next(err);
        if(question.length === 0) return next(errorHandler(404, "Question not found"));
        res.json(question);
    });
}

When I console.log(question) that's returned from the $resource method I get a normal promise object: enter image description here

Upvotes: 0

Views: 162

Answers (1)

PrimosK
PrimosK

Reputation: 13918

There is a typo in:

<h3>Question:</h3>
<p>{{ question.question }}</p>
<ul>
    <li np-repeat="answer in question.answers">
        <span ng-show="answer.is_true">✓</span>
        <span>{{ answer.content }}</span>
    </li>
</ul>

There should be ng-repeat instead of np-repeate.

Upvotes: 4

Related Questions