的基本思想是为具有范围变量,将节省被单击的按钮(通过ng-click
属性)和有条件地分配一个css
类(通过ng-class
属性)匹配链接。
HTML文件:
<div ng-app="myApp" ng-controller="myCtrl as vm">
<div class="wrapper col-md-12 col-sm-12">
<!--
when you click the link, it will set $scope.type to the define type
('week' in the first case),
we then compare $scope.type with some hardcoded value to set the
class and change the css properties accordingly)
Of course, you could extract this to a $scope method
-->
<a title="week? category" ng-class="{active: vm.type=='week'}" ng-click="vm.type = 'week'" class="tag">week</a>
<a title="week? category" ng-class="{active: vm.type=='month'}" ng-click="vm.type = 'month'" class="tag">month</a>
<a title="week? category" ng-class="{active: vm.type=='year'}" ng-click="vm.type = 'year'" class="tag">year</a>
</div>
</div>
的Javascript:
app.controller('myCtrl', function($scope) {
$scope.type = null;
});
的CSS(为active
类):
.tag:hover, .active {
background-color: red;
border-color: red;
}
见Forked plknr