我想问一下,如何在Angularjs中更改div中单击项目的颜色:
<div ng-repeat="answers in answer track by $index"
ng-click="addToAnswers(currentPage,$index)">{{answers}}</div>
这是测验答案。 像这里:a link “jquery点击切换”,但我不想使用jquery。
答案 0 :(得分:3)
首先请将answers in answer
更改为answer in answers
。您可以在isChecked
element
上拥有answers
属性
<div ng-repeat="answer in answers track by $index"
ng-class="answer.isChecked? 'red': 'green'"
ng-click="addToAnswers(currentPage,$index);answer.isChecked != answer.isChecked">
{{answer}}
</div>
Selected Answers{{ answers | filter: {isChecked: true}}}
<强> CSS 强>
.red {
background-color: red;
}
.green {
background-color: green;
}
答案 1 :(得分:0)
只需使用ng-class
。
在ng-click
函数中,您需要将范围&lt; clicked
变量设置为true。这样,您可以在点击红色类时显示红色类,否则显示绿色类。
$scope.clicked = false;
$scope.addToAnswers = function(page, index) {
page.clicked = true;
});
<div ng-repeat="answers in answer track by $index"
ng-click="addToAnswers(currentPage,$index)"
ng-class="{'back-red': currentPage.clicked, 'back-green': !currentPage.clicked}">{{answers}}</div>
.back-red { background-color: red; }