AngularJS ng-class和ng-style一起工作

时间:2016-05-01 17:33:33

标签: javascript css angularjs

我的div中有一个ng-repeat:

<div ng-repeat="el in list">
    <button 
     ng-class="{ 'selected' : selectedButton == $index }" 
     ng-style="{ 'background-color' : userColor }"
     >
       {{el}}
    </button>
</div>

在css文件中:

.selected{
   background-color: red;
}

想法是将背景颜色红色设置为最后按下的按钮。并且所有其他按钮应具有用户之前选择的背景颜色。

问题是,当ng-style中的背景颜色已经应用时,它不会用选定的类background-color覆盖它。我必须使用selectedButton == $ index,因为其他东西都依赖于它。

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

在课堂上尝试使用!important。这可能会解决您的问题。

.selected{
   background-color: red !important;
}

内联样式优先于类的css,直到!important应用。

来自CheezyCode的欢呼声

相关问题