我的HTML
<li data-ng-click="sortCategory('demographics')">Demographics</li>
<li data-ng-click="sortCategory('historical')">Historical</li>
<li data-ng-click="sortCategory('experimental')">Experimental</li>
我试过了
<li data-ng-click="sortCategory('socialmedia'); select(item)" data-ng-class="{active: isActive(item)}">Social Media</li>
$scope.isActive = function (item)
{
return $scope.isActive(item);
}
CSS(更少)
li{
&.active {
background:#0088cc;
}
}
它不起作用...
最佳答案
完全同意Yoshi的说法,您可以在调用sortCategory函数时将类别存储在作用域变量中,并使用它来检查类别是否处于活动状态。一段有效的代码可能是:
$scope.category = 'demographics'; //Your default category
$scope.sortCategory = function (category) {
$scope.category = category; //Update the category
....
}
$scope.isActive = function (category) {
//Check if category of a given <li> is equal to the current category
return $scope.category === category;
}
和看起来像的模板标记
<li data-ng-click="sortCategory('demographics')" ng-class="{'active' : isActive('demographics')}">Demographics</li>
<li data-ng-click="sortCategory('historical')" ng-class="{'active' : isActive('historical')}">Historical</li>
您可以在此处找到实时示例http://jsfiddle.net/sWKL5/2/
关于javascript - 如何在Angular JS中更改事件链接的背景颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18652499/