在 angular.js 中,如何在点击元素时打开和关闭css类active
?我知道我可以使用ng-class
和ng-click
,但我如何引用自己?例如,我们说我有三个按钮:
<a class="btn" ng-click="" ng-class"">First</a>
<a class="btn" ng-click="" ng-class"">Second</a>
<a class="btn" ng-click="" ng-class"">Third</a>
答案 0 :(得分:1)
<a ng-click="firstActive = !firstActive" ng-class"{'btn':true, 'active':firstActive}">First</a>
<a ng-click="secondActive = !firstActive" ng-class"{'btn':true, 'active':secondActive}">Second</a>
<a ng-click="thirdActive = !firstActive" ng-class"{'btn':true, 'active':thirdActive}">Third</a>
您必须在控制器中为firstActive,secondActive和thirdActive设置单独的范围变量,默认情况下都设置为false。