我有以下情况,我正在建立一个测验,我有一些问题,有些问题有孩子,这取决于你回答的问题我会表达对应答案的问题。
例如:
问题标题:
- 答案1
- 答案的另一个问题1
- 答案2
醇>
- 答案的另一个问题2
因此,当您按答案1时,您将看到另一个问题,答案为1'或者,如果按答案2,您将看到'另一个问题,答案为2'
我的应用结构如下所示:
$scope.currentIndex = 0;
$scope.questions = [{
title: 'Question title',
choices: ['Answer 1', 'Answer 2'],
extra: {
childs: [{
link: 'answer-1',
title: 'Another question with answers 1',
choices: ['Some answer', 'Some answer 2']
}, {
link: 'answer-2',
title: 'Another question with answers 2',
choices: ['Some answer', 'Some answer 2']
}]
}
}, {
title: 'Simple question title',
choices: ['Answer 1', 'Answer 2']
}]
在我的Angular应用程序中,我使用以下方式显示问题:
ng-repeat="question in questions track by $index" ng-if="isCurrentQuestion($index)"
isCurrentQuestion()
函数如下所示:
$scope.isCurrentQuestion = function(index) {
return $scope.currentQuestion === index
};
每次我回答问题时,我都会向$ scope.currentIndex添加+1以显示下一个问题。
我不明白的是如何在我的应用中显示(question.extra.childs
)的孩子问题?
也许我的结构非常正确,但我很开心。
答案 0 :(得分:1)
使用嵌套的ng-repeat方法来解决您的需求。
<div ng-repeat="question in questions">
// Do your logic here
// use ng-if for your condition, if its true it will show your child elements
<div ng-if="question.condition" ng-repeat="extraquestions in question.extra.childs">
// Do your logic here
</div>
</div>
答案 1 :(得分:0)
您可以尝试在当前版本中添加另一个ng-repeat,如
<div ng-repeat="question in questions track by $index" ng-if="isCurrentQuestion($index)">
.......
<div ng-repeat="extraquestions in question.extra.childs">
.......
</div>
</div>