我在angularjs中使用ng-repeat创建一个表格,我想在按钮点击时更改行的背景颜色。 喜欢点击是绿色应该设置,并且没有按钮红色设置。 请有人帮忙吗
答案 0 :(得分:1)
每行都有一个ID。您所要做的就是将此ID发送到函数setSelected(),
存储它(例如在$scope.idSelectedVote
中),然后检查每一行是否所选ID与当前ID相同。这是一个解决方案(见the documentation or ngClass, if needed);
$scope.idSelectedVote = null;
$scope.setSelected = function (idSelectedVote) {
$scope.idSelectedVote = idSelectedVote;
};
<ul ng-repeat="vote in votes" ng-click="setSelected(vote.id)" ng-class="{selected: vote.id === idSelectedVote}">
...
</ul>
答案 1 :(得分:0)
<ul ng-repeat="vote in votes" ng-click="setSelected(vote.id)" ng-class="
{selected: vote.id === idSelectedVote}">
...
</ul>
控制器中的
$scope.idSelectedVote = null;
$scope.setSelected = function (idSelectedVote) {
$scope.idSelectedVote = idSelectedVote;
};
参阅:http://plnkr.co/edit/SkaYSbtKFdx1I9N0xP5E?p=preview