我有这个对象:
{
"_id" : ObjectId("5454e173cf8472d44e7df161"),
"questionType" : 0,
"question" : "question1",
"answers" : [
{
"content" : "answer1"
},
{
"is_true" : "true",
"content" : "answer2"
},
{
"content" : "answer3"
}
],
"matches" : [],
"__v" : 0
}
这个控制器:
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);
});
}
当console.log(question)
从$resource
方法返回时,我得到一个普通的promise对象:
答案 0 :(得分:4)
有一个拼写错误:
<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
。