单击更改类到选定按钮

时间:2015-04-24 14:51:43

标签: angularjs angularjs-ng-click ng-class

我想将所选按钮的类btn-white更改为btn-primary

$scope.SelectedCombination = function (combinationId) {
        $scope.selectedCombination =combinationId;
    };

HTML

<button ng-repeat="combination in combinations" class="btn btn-white" ng-model="" value="{{combination.combinationId}}" ng-click="SelectedCombination(combination.combinationId)"  type="button">{{combination.name}}</button>

2 个答案:

答案 0 :(得分:1)

使用ng-class。不要使用任何javascript函数来设置css。

<button ng-repeat="combination in combinations" class="btn btn-white" ng-model="x" value="{{combination.combinationId}}" ng-click="SelectedCombination(combination.combinationId);clicked=true;" ng-class="{btn-primary:clicked}" type="button">{{combination.name}}
</button>

或者您可以使用最佳方法:

<button ng-repeat="combination in combinations" class="btn btn-white" ng-model="x" value="{{combination.combinationId}}" ng-click="SelectedCombination(combination.combinationId);" ng-class="{'btn-primary':selectedCombination==combination.combinationId}" type="button">{{combination.name}}
</button>

请参阅plunker

答案 1 :(得分:0)

有一种叫做Ng-Class的东西。 Ng-class,允许您通过数据绑定表示要添加的所有类的表达式,在HTML元素上动态设置CSS类.class