Search code examples
angularjsng-styleangularjs-ng-style

change CSS based on value Angularjs


I want to change label color and cursor type based on ng-model

index.html

<button ng-click="changeType()">Change</button>
<label class="fee-type" ng-style="feeType ==='one' && {'class':'disabled-class'}" for="fixed2">Client Contengency Fee</label>
<label class="fee-type"  ng-style="feeType ==='one' && {'class':'disabled-class'}" for="fixed1">Fixed Fee Per Property</label>

The default class is fee-type but when then button get clicked its class should change to disabled-class

index.css

.fee-type {
    float: left;
    clear: none;
    display: block;
    padding: 2px 1em 0 0;
    color: black;
}

.disabled-class {
    color:gray;
    cursor: not-allowed;
}

index.js

$scope.feeType= two;

$scope.changeType= function(){
    $scope.feeType=one;
}

Solution

  • You should use ng-class instead of ng-style and add condition in ng-class to apply css class in your label.

    HTML:

    <button ng-click="changeType()">Change</button>
    <label class="fee-type" ng-class="{'disabled-class':feeType ==='one'}" for="fixed2">Client Contengency Fee</label>
    <label class="fee-type" ng-class="{'disabled-class':feeType ==='two'}" for="fixed1">Fixed Fee Per Property</label>
    

    and controller

    $scope.feeType = 'two';
    
    $scope.changeType = function() {
       $scope.feeType = 'one';
    };