2014-11-01 130 views
0

我有這樣的對象:ngRepeat不循環數組

{ 
    "_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); 
    }); 
} 

當我認爲是一個從$resource方法返回,我得到一個正常的諾言對象console.log(question)enter image description here

+0

Question.get返回什麼?你能顯示Question.get方法的代碼嗎? – 2014-11-01 13:46:39

+0

@MichalCharemza完成後,請再次檢查問題。 – 2014-11-01 13:54:11

+0

你可以steup plunkr ..我rfeally沒有看到任何問題的代碼... – harishr 2014-11-01 13:54:14

回答

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

+0

哦哇!這真是令人尷尬和令人印象深刻的注意!當然解決了它。非常感謝。 – 2014-11-01 14:03:00

+1

不客氣。我們是人類..每個人都犯錯誤。 – PrimosK 2014-11-01 14:03:26