如何更改Angular js中活动链接的背景颜色

时间:2013-09-06 07:46:42

标签: javascript css angularjs

我的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(less)

li{
&.active {
  background:#0088cc;
}
}

它不起作用......

1 个答案:

答案 0 :(得分:2)

完全同意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/