的基本思想是爲具有範圍變量,將節省被單擊的按鈕(通過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