{ "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先生的解决方案是解决我的问题的答案。 –