我有这个对象:
{
"_id" : ObjectId("5454e173cf8472d44e7df161"),
"questionType" : 0,
"question" : "question1",
"answers" : [
{
"content" : "answer1"
},
{
"is_true" : "true",
"content" : "answer2"
},
{
"content" : "answer3"
}
],
"matches" : [],
"__v" : 0
}
这个 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]);
这个标记:
<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>
但不幸的是,生成的 html 不包含 JSON 中的任何答案。这意味着 ngRepeat
没有循环遍历 question.answers
数组。
<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>
知道如何解决这个问题吗?
<小时/>编辑
这是问题工厂代码:
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]);
其中restUrls.question.index
是“/api/questions/:id”
以及用于获取单个问题的node.js后端代码:
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);
});
}
当我从 $resource
方法返回的 console.log(question)
时,我得到一个正常的 Promise 对象:
最佳答案
有一个拼写错误:
<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>
应该有 ng-repeat
而不是 np-repeate
。
关于javascript - ngRepeat 不循环数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26689856/