在Angular.js中单击元素上的一个类

时间:2014-07-31 23:05:25

标签: javascript angularjs

angular.js 中,如何在点击元素时打开和关闭css类active?我知道我可以使用ng-classng-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> 

1 个答案:

答案 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。