{ "questions": [
{
"text": "Which colors do you see in the rainbow?",
"answer": "A",
"A": "RED",
"B": "ORANGE",
"C": "YELLOW",
"D": "GREEN"
},
{
"text": "What is sanatbek's family name?",
"answer": "C",
"A": "Saidov",
"B": "Muhammadlatipov",
"C": "Matlatipov",
"D": "Boboyev"
},
{
"text": "How many members does this company have?",
"answer": "C",
"A": "3",
"B": "8",
"C": "9",
"D": "2"
}
]
}
這是我JSON
文件,我需要得到的問題長度AngularJS
這樣的:獲取JSON的長度,角度
function getQuestionCount() {
return $scope.questions.length;
}
這裏$scope.questions
在控制器中聲明: 我嘗試了好幾種像
return Object.keys($scope.questions).length
這是returning value: 6
;顯示後console.log($scope.questions)
。我明白這是我的Parsed Object的屬性數量。- 然後想了一下,我試了這
return $scope.questions.data.questions.length;
這是工作。但是,以下錯誤顯示在我的控制檯上。
這裏是我的錯誤消息:
TypeError: Cannot read property 'data' of undefined.
什麼是'$ scope.questions'?它是三個對象的數組嗎? –
是的,它應該是從'questions'鍵取得的三個對象的數組。 Raghu先生的解決方案是解決我的問題的答案。 –