简体   繁体   English

角度| 删除按钮上的活动课程

[英]Angular | Remove active class on button

I'm making an AngularJS project using Bootstrap. 我正在使用Bootstrap制作AngularJS项目。

Here's the JSfiddle: https://jsfiddle.net/mijacat/dmuuu79v/3/ 这是JSfiddle: https ://jsfiddle.net/mijacat/dmuuu79v/3/

I'd like to be able to toggle the button to have it instantly change back and forth from .btn btn-default to .btn btn-info . 我希望能够切换按钮,使其立即从.btn btn-default.btn btn-info However, usually the button is 'active', then it changes state on a second click. 但是,通常按钮是“活动的”,然后在第二次单击时更改状态。

How can I do this in one click? 一键执行该如何操作? Is there a solution using Angular if possible? 如果可能,是否有使用Angular的解决方案? This is because I'm hoping to change the button state to something different later. 这是因为我希望以后将按钮状态更改为其他状态。

Just use ng-class : 只需使用ng-class

<button class="btn" ng-class="{'btn-info': some_condition}">...</button>

Here's an updated JSFiddle (also note the usage of $index variable): https://jsfiddle.net/dmuuu79v/4/ 这是更新的JSFiddle(还请注意$index变量的用法): https ://jsfiddle.net/dmuuu79v/4/

The problem you have is that you have a one variable $scope.isToggled to keep the state of all buttons. 您遇到的问题是,您有一个变量$scope.isToggled来保持所有按钮的状态。 You should keep it separate. 您应该将其分开。

I have corrected your fiddle: https://jsfiddle.net/dmuuu79v/5/ 我已经纠正了你的小提琴: https : //jsfiddle.net/dmuuu79v/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM