根据值Angularjs更改CSS

时间:2017-08-16 12:33:54

标签: angularjs ng-style angularjs-ng-style

我想根据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>

默认课程为费用类型,但是当点击按钮时,其课程应更改为禁用课程

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;
}

1 个答案:

答案 0 :(得分:2)

您应该使用ng-class代替ng-style并在ng-class中添加条件以在您的标签中应用css类。

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>

和控制器

$scope.feeType = 'two';

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