javascript - ngRepeat 不循环数组

标签 javascript json angularjs node.js mongodb

我有这个对象:

{
    "_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 对象: enter image description here

最佳答案

有一个拼写错误:

<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/

相关文章:

javascript - 如何将获取到的记录一条一条的显示出来

javascript - 无法在我的 iOS 设备上安装 React Native 应用程序

Android - 如何使用 SQLite 缓存 Json

mysql json 字段 json_remove()

javascript - 如何让$watch只触发一次?

javascript - AngularJS 如何将 RESTful 与工厂模块一起使用

javascript - 如何在 ASP.net MVC 3 中成功上传文件后显示警报

javascript - Jquery mobile - 如何防止可折叠的 div 在点击后展开?

Javascript:对象文字表示法中的indexOf

javascript - JSON 字符串中的位置到对象中的路径