我是AngularJS的新手,需要开发一个问卷。一开始,我们只能访问A类问题,其他问题都是"只读"的。要访问B类问题,我们需要给出A类问题的正确答案等。顺序可以改变:这意味着B类问题可以因为不同的原因排在A类问题之前。
我的逻辑:forEach(q in questions){ //iterate all questions
if(q.getCategory() == 'A' && q.isCorrect()){ // I have a function which check if question with category is 'A' and if answer of question with category'A' is correct
for(q in questions){ //as the order of questions can change I need iterate again all question to find question with category 'B'
if(q.getCategory() =='B'){
do something //I want to also change css
}
}
}
};
我们如何使用AngularJS来实现这些呢?我应该在视图中做这个还是应该创建一个指令?
<!DOCTYPE html>
<html ng-app="myModule">
<head>
<title></title>
<script src="Scripts/angular.js"></script>
<script src="Scripts/MyModuleScript.js"></script>
</head>
<body ng-controller="myController">
<div ng-repeat="q in questions">
<span> {{q.content}}</span>
<div ng-if=q.getCategory() == 'A' && q.isCorrect()>
<div ng-repeat="q in questions">
<div if(q.getCategory() =='B') class ="..">
</div>
</div>
</div>
</div>
</body>
</html>
试试这个,你可以使用ng-class
或ng-style
来改变css。
<div ng-repeat="a in questions | filter:{'category':'A', 'isCorrect': true}">
<div ng-repeat="b in questions | filter:{'category':'B'}">{{b}}</div>
</div>
正如ram所说,他给了你灵感。但是最好按照下面的方式使用内部的ng-repeat。
<div ng-repeat="b in a| filter:{'category':'B'}">{{b}}</div>